Help for for loop!

void Test::Test2()
{
int menuChoice;
cout<<"Welcome!"<<endl;
cout<<" "<<endl;

cout<<"1) Choice1"<<endl;
cout<<"2) Choice2"<<endl;
cout<<"3) Choice3"<<endl;
cout<<"4) Choice4"<<endl;
cout<<" "<<endl;
cout<<"Please enter your choice: ";cin>>menuChoice;

if(menuChoice == 1)
{
//array storage
int storeName;
const int SIZE = 99;
char choice;
ABC arrayABC[SIZE];

int x;
for(x=0; x<SIZE; ++x)
{
cout<<"[name]"<<endl;
cout<<"Please enter name :";
cin>>storeName;
arrayABC[x].SetName(storeName);

//ending of loop
cout<<"Is that all? (y / n) :";
cin>> choice;
if(choice == 'y')
{
return 1;
}
else if(choice == 'n')
{
break;
}
}


}
else if(menuChoice == 2)
{
cout<<"You choosen 2"<<endl;
}
else if(menuChoice == 3)
{
cout<<"You choosen 3"<<endl;
}
else if(menuChoice == 4)
{
cout<<"You choosen 4"<<endl;
}
else if(menuChoice != 1 || menuChoice != 2 || menuChoice != 3 || menuChoice != 4)
{
cout<<"Please choose again!"<<endl;
}
}

how do i stop the for loop if the person enters 'n'?
and how do I continue if the person enters 'y'?

please help!!
replace return 1; with break;

replace break; with continue;
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
void Test::Test2()
{
int menuChoice;
cout<<"Welcome!"<<endl;
cout<<" "<<endl;

cout<<"1) Choice1"<<endl;
cout<<"2) Choice2"<<endl;
cout<<"3) Choice3"<<endl;
cout<<"4) Choice4"<<endl;
cout<<" "<<endl;
cout<<"Please enter your choice: ";cin>>menuChoice;

if(menuChoice == 1)
{
//array storage
int storeName;
const int SIZE = 99;
char choice;
ABC arrayABC[SIZE];

int x;
for(x=0; x<SIZE; ++x)
{
cout<<"[name]"<<endl;
cout<<"Please enter name :";
cin>>storeName;
arrayABC[x].SetName(storeName);

//ending of loop
cout<<"Is that all? (y / n) :";
cin>> choice;
if(choice == 'y')
{
return 1;
}
else if(choice == 'n')
{
break;
}
}


}
else if(menuChoice == 2)
{
cout<<"You choosen 2"<<endl;
}
else if(menuChoice == 3)
{
cout<<"You choosen 3"<<endl;
}
else if(menuChoice == 4)
{
cout<<"You choosen 4"<<endl;
}
else if(menuChoice != 1 || menuChoice != 2 || menuChoice != 3 || menuChoice != 4)
{
cout<<"Please choose again!"<<endl;
}
}
int storeName; cin>>storeName;
How come?

Have you tried using switch loop?
should be string.
sorry.

i dont know how to use switch loop. any tutorial recommendations?
http://www.cplusplus.com/doc/tutorial/control/

Switch is at the end of the page. If you don't understand something just ask :)
will it be better with switch? and if should my array be somewhere else since i need to print the information in it out on other menuChoice.
I don't know which is more efficient between switch and else-if, but for this kind of programs, where you input a number to make a selection, switch is usually used (with a case to handle invalid input). I don't know why, maybe educational purpose

Your array will be created only if the user choice == 1, so if you need it in multiple cases you have to define it outside the conditional statement
Last edited on
void Test::Test2()
{
int menuChoice;
cout<<"Welcome!"<<endl;
cout<<" "<<endl;

cout<<"1) Choice1"<<endl;
cout<<"2) Choice2"<<endl;
cout<<"3) Choice3"<<endl;
cout<<"4) Choice4"<<endl;
cout<<" "<<endl;
cout<<"Please enter your choice: ";cin>>menuChoice;

if(menuChoice == 1)
{
//array storage
int storeName, record =0;
const int SIZE = 99;
char choice;
ABC arrayABC[SIZE];

int x;
for(x=0; x<SIZE; ++x)
{
cout<<"[name]"<<endl;
cout<<"Please enter name :";
cin>>storeName;
arrayABC[x].SetName(storeName);
++record
cout<<"*****"<<record<<" record successfully stored "<<endl;
//ending of loop
cout<<"Is that all? (y / n) :";
cin>> choice;
if(choice == 'y')
{
break;
}
else if(choice == 'n')
{
continue;
}
}


}
else if(menuChoice == 2)
{
cout<<"Total number of records :"<<record<<endl;
}
else if(menuChoice == 3)
{
cout<<"You choosen 3"<<endl;
}
else if(menuChoice == 4)
{
cout<<"You choosen 4"<<endl;
}
else if(menuChoice != 1 || menuChoice != 2 || menuChoice != 3 || menuChoice != 4)
{
cout<<"Please choose again!"<<endl;
}
}

i have a new problem.
is is regarding the array.
when i choose menu 1 and input the name, records are being updated accordingly to how many names i am inserting.

is there any way to check if records are being stored into the array?

and when i choose menu 2, to print out the records of name, record is 0 there. so I am assuming, when choosing menu 2, i cannot access the arrays of data stored in menu 1.

is there any solutions that I can store/access the information into the array properly and able to access it any where?
A question before everything else: does your code get compiled? Because I tried copying it and MinGW won't do it

error: 'record' was not declared in this scope

This refers to the line right below "else if(menuChoice == 2)"

Everything that follows is under the assumption that you fixed this problem

is there any way to check if records are being stored into the array?


1
2
for(int i = 0; i < record; ++i)
  cout << arrayABC[i].name << endl;

This will print every value you gave as a name for all the elements you have created

is there any solutions that I can store/access the information into the array properly and able to access it any where?

I'll assume that by "anywhere" you mean anywhere within Test2()
Just define all the variables at the start of the function (by the way, this will fix the problem thrown by the compiler)
As of now you defined the variables inside the if block, so, just like for functions, they belong to the scope of the block and you can't use them outside of it
If you define them outside they'll still exist when you exit the if block (you just changed their value in that) so you'll be able to use them in other parts of the function
and when i choose menu 2, to print out the records of name, record is 0 there. so I am assuming, when choosing menu 2, i cannot access the arrays of data stored in menu 1.

I'll assume that you have some sort of loop that calls your Test::Test2() multiple times, because if you don't then the program will end before you have the chance of choosing the option 2 after choosing 1

Try fixing the scope of the variables and see if there is still this problem

Also, please use code tags and proper indentation when you write code here
Last edited on
Topic archived. No new replies allowed.