switch statements
supcello (10)
Dec 12, 2012 at 9:51am UTC
Hey, ran into a little bit of a problem.
I'm making a menu that uses switch statements which uses function prototypes.
I guess my question is, do I have to write my switch before int main? or can it be included in my int main?
Fransje (178)
Dec 12, 2012 at 10:02am UTC
You can write it anywhere you like.
supcello (10)
Dec 12, 2012 at 10:04am UTC
Okay thanks. I'm having a problem with the switch menu. This is my code (not done).
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
#include <iostream>
using namespace std;
void rows(int );
int main()
{
int option;
cout << "**** Menu ****" << endl;
cout << "(1) 100 x's" << endl;
cout << "(2) celsius to fahrenheit" << endl;
cout << "(3) fahrenheit to celsius" << endl;
cout << "(4) servers tips" << endl;
cout << "(5) quit" << endl;
switch (option)
{
case 1:
void rows();
break ;
case 2:
cout << "test" << endl;
break ;
}
char x;
cin >> x;
return 0;
}
void rows()
{
int i,j,y;
for (int i = 0; i < 10; ++i)
{
for (int j = 0; j < 10; ++j)
cout << 'X' ;
cout << endl;
}
int a;
cin >> a;
}
Fransje (178)
Dec 12, 2012 at 10:09am UTC
You first have to get the value of option from the user: cin >> option; . Then you can start checking option . Otherwise option will have some garbage value.
Last edited on Dec 12, 2012 at 10:09am UTC
supcello (10)
Dec 12, 2012 at 10:13am UTC
Duh, I knew that... Okay maybe another question that'll be a little harder to answer?
I select 1 and it just runs the program super quick and exits the console.
firedraco (5413)
Dec 12, 2012 at 10:20am UTC
That's because line 19 is not a call to the function rows, it is a random function prototype (which does nothing).
supcello (10)
Dec 12, 2012 at 10:22am UTC
LIttle confused, could you explain more?
Line 19 doesn't call rows because why?
firedraco (5413)
Dec 12, 2012 at 10:23am UTC
The same reason line 4 does not call rows.
supcello (10)
Dec 12, 2012 at 10:27am UTC
Does int main need to call rows? I thought it was 'prepped' for rows and then it could be called when case 1 was chosen?
supcello (10)
Dec 12, 2012 at 11:07am UTC
I figured out, why iit wasn't calling rows. Thanks.
supcello (10)
Dec 12, 2012 at 11:28am UTC
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
#include <iostream>
using namespace std;
void rows();
void convert();
int main()
{
int option;
cout << "**** Menu ****" << endl;
cout << "(1) 100 x's" << endl;
cout << "(2) celsius to fahrenheit" << endl;
cout << "(3) fahrenheit to celsius" << endl;
cout << "(4) servers tips" << endl;
cout << "(5) quit" << endl;
cin >> option;
switch (option)
{
case 1:
rows();
break ;
case 2:
convert();
break ;
}
return 0;
}
void rows()
{
int i,j,y;
for (int i = 0; i < 10; ++i)
{
for (int j = 0; j < 10; ++j)
cout << 'X' ;
cout << endl;
}
}
void convert()
{
float celsius, fahrenheit;
cout << "enter the temperature" << endl;
cin >> fahrenheit;
fahrenheit = ((9.0/5.0) * celsius) + 32.0;
cout << fahrenheit;
}
I get an error after entering a number for the convert()