Need help with an assignment

So I have only been coding for a week and in the add_values function I need to allow the user to return to the display_menu function before having filled in all 10 numbers. Can anyone help please?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

void display_menu(void)
{
    while(1)
        {

    cout << "(1) Add a value" << endl;

    cout << "(2) Edit a value" << endl;

    cout << "(3) Print a value" << endl;

    cout << "(4) Display Statistics" << endl;

    cout << "(5) Quit the program" << endl;

    cout << "Enter choice please." << endl;

    cin >> user_input;

    if (user_input == 1)
    {
        add_values (); continue;
    }

    if (user_input == 2)
    {
        edit_value();
    }

    if (user_input == 3)
    {
        print_value();
    }

    if (user_input == 4)
    {
        display_stats();
    }

    if (user_input == 5)
    {
        exit_program();
    }

    if ((user_input < 1) || (user_input > 5))
    {
        cout << "/a/a/a/a/a" << "Enter a correct option." << endl;
    }

    }
    return;
}


void add_values ()
{
    cout << "Please add 10 numbers between 999 to -999." << endl;

    cout << "Please input exit if you wish to return to menu." << endl;

    char exit = 1;

    for(int i=0; i<10; i++)
    {
        cin >> user_num[i];
    }

    if (cin >> exit)
    {
        display_menu();
    }

    return;
}

void edit_value()
{
    int edit_value,index;

    cout << endl << "Please select a value to edit by typing [a number from 0 to 9]" <<
     "which will call up the correct user_num[index] thank you" << endl;

    cin >> index;

    cout << endl << "Enter Value to replace at user_num["<<index<<"]" << endl;

    cin >> edit_value;

    user_num[index] = edit_value;

    cout << endl << "Value has been replaced at index ["<<index<<"]" << endl;

    return;
}
1
2
3
4
5
6
7
8
9
10
void display_menu(void)
do
{
    //all that stuff 
    
    char choice;
    cout<<"Do you want to see the menu again? y for YES, any other for NO :";
    cin>>choice;
}while(choice == 'y');
return ;
Its in the add_values part that I need to be able to leave add_values early and go to display_menu so will that code still work?
Try doing this...

for (int i = 0; i < 10; i++)
{
if (!(cin >> user_num[i]))
return;
}

I do not know if this will work, but my hypothesis is that if the user enters "exit" instead of a value for user_num then cin will return false and the '!' will negate false into true running the if statement and returning
return exits any function back to its caller.
Last edited on
would this work?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void add_values ()
{
    cout << "Please enter 10 numbers between -999 to 999." << endl;//Changed "Please add", to "Please enter"
    
    cout << "Enter a number greater than 999, or leess than -999 if you wish to return to the menu." << endl;
    
    int user_num[10];
    
    for(int i=0; i<10; i++)
    {
    
        cin >> user_num[i];
        
        if ((user_num[i] < -999) || (user_num[i] > 999))
        {
            display_menu();
        }
        else
        {
            cin >> user_num[i];
        }
    }
    
    
    return;
}
Yes I know what return does.. but not so sure with the if expression... like how it works
Does cin return true if it succeeds and false if it fails?
The one problem I noticed right away with this code is if the user exits before entering all the values and then returns to the add_values function, then he will have to reenter 10 values and just be rewriting the values
Also... lets say he uses types exit, wont the the user_num values be corrupted?
yeah i think if the user exits before entering the full array of 10 integers, however many the user didn't enter will go unassigned
If he says exit after entering 3 variables then wont c++ try to assign "exit" to user_num[3]? And wouldnt that be bad? lol
This piece of code tests, fail bits and the return values of cin.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main()
{
    int test;
    cout<<"Enter a string: ";
    if(!(cin>>test))
        cout<<"fail bit set";
    else
        cout<<"no fail bit set";
    return 0;
}


Enter a string: shadowCODE
fail bit set


Hence this can be used on your program with no fear @coltehrman suggested.

1
2
3
4
5
for (int i = 0; i < 10; i++)
{
   if (!(cin >> user_num[i]))
   return;
}
Topic archived. No new replies allowed.