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?
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 ;
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
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;
}
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
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>
usingnamespace 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;
}