Switch Issues

I have been having some issues with my switch loop.

All the code that precedes it runs perfectly.

But when i get to the point where i need to input an option, i type in an option, and it seems to interpret that as an invalid option, and goes straight to the default section of the switch statement. (removing the default portion does not work).

Can anybody help me.

I am using Bloodshed Dev-C++.


I would post the code here, but it is too long (sorry).

Code: http://67.86.125.205:50001/School/all.zip


Thanks,
Stephen
Last edited on
Can you post just the "switch" portion of the code, along with any preceeding variables/values that might effect it?

On a seperate note, Dev-C++ hasn't been updated in about 5 years. Try something else, such as Code::Blocks, or wxDevC++ (made by the same team as C++). I prefer CB myself, but haven't tried wxDev so I can't offer any real argument from one over the other.

hth
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182


#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <unistd.h>
#include <conio.h>

using namespace std;

main()

{
     int number1, number2; //Variable, math_1
     int number3, number4; //Variable, name_and_math_1
     int number5=0, number6=0; //Variable, for_loop_1
     char fname[20], lname[20]; //Variable, for name storage
     char answer, answer1, answer2, answer3; //Variable
     char select; //Variable
     char sure; //Variable
     char secret; //Variable
     char junk; //Not sure what this does but it makes things work





//theres some gpl info and stuff here





     printf("Please enter your first name:\t"); //Ask user to enter their first name
     scanf("%s",&fname); //Read/store first name
     printf("Please enter your last name:\t"); //Ask user to enter their last name
     scanf("%s",&lname); //Read/store last name
     printf("Hi %s",fname); //Say hi
     printf(" %s \n",lname); //Im really quite formal, arne't I


     
          top: //Goto destination 1
          
          system("cls"); //Clear screen
     
          printf("Please select a program %s",fname); //Information/instructions
          printf("\n1 Some Simple Addition"); //Information/instructions
          printf("\n2 Some Simple Addintion That Tells Your Your Name"); //Information/instructions
          printf("\n3 Can You Count To One Million?"); //Information/instructions
          printf("\n4 *******"); //Information/instructions
          printf("\n5 *******"); //Information/instructions
          printf("\n6 *******"); //Information/instructions
          printf("\n7 *******"); //Information/instructions
          printf("\n8 *******"); //Information/instructions
          printf("\n9 *******"); //Information/instructions
          
          
          printf("\n"); //New line
          scanf("%c", &answer); //Wait for user input
          scanf("%c", &junk); //Not sure what this does but it makes things work
          printf("\n"); //New line
          
          switch (answer) //Begin switch statement
                 {
                         case '1': //First option
                         //math_1
                              system("cls");
                              cout << "number 1? ";
                              cin >> number1;
                              cout << "number 2? ";
                              cin >> number2;
                              answer = number1 + number2;
                              cout << "\nAnswer is " <<  answer1;
                              printf("\n");
                              system ("pause");
                              printf("\n");
                              printf("\n");
                              goto top; //Return to "top:"
                         break; //End statement
                              
                         case '2': //Second option
                         //name_and_math_1
                              printf("Hi %s \n",fname);
                              Beep(2000,200); //Tells user that section has finished
                              system("pause"); //Press (the) ANY key to continue
      
      
                              printf ("Ok, Now for some simple math!!\n"); //Tells the user whats happening
                              cout << "Number 1? "; //User input number1
                              cin >> number3; //Take number1 and store it in memory
                              cout << "Number 2? "; //User input number2
                              cin >> number4; //Take number2 and store it in memory
                              answer = number3 + number4; //Add number1 and number2 together
                              cout << "Answer is " <<  answer2; //Show answer of above function
                              Beep(2000,200); //Tells user that section has finished
                              system ("pause"); //Press (the) ANY key to continue
                              goto top;
                              
                         break; //End statement
                              
                         case '3': //Third option
                         //for_loop_1
                              printf("Ok, now it's time for some simple counting.\n");
                              system("pause");
                              printf("Can you keep up........\n");
                              system("pause");
                              for (number5=0; number5 <1000001; number5++) //number <=10 or number <11
                              {
                               printf("%d ", number5);
                              }
                              printf("\n");
                              system ("pause");
                         break; //End statement
                         
//                         case '4': //Fourth option
//                              
//                              
//                         break; //End statement
                         








                              
                        default:
                                Beep(2000,200); //Tells the user that the comand has been run
                                Beep(2000,200); //Tells the user that the comand has been run
                                Beep(2000,200); //Tells the user that the comand has been run
                                Beep(2000,200); //Tells the user that the comand has been run
                                Beep(2000,200); //Tells the user that the comand has been run
                                printf("-__-\n"); //Information/instructions
                                printf("."); //Information/instructions
                                Sleep (800); //ZzzZzzZzzZzz
                                printf("."); //Information/instructions 
                                Sleep (800); //ZzzZzzZzzZzz
                                printf("."); //Information/instructions
                                Sleep (800); //ZzzZzzZzzZzz
                                printf("."); //Information/instructions
                                Sleep (800); //ZzzZzzZzzZzz
                                printf("."); //Information/instructions
                                Sleep (800); //ZzzZzzZzzZzz
                                printf("."); //Information/instructions
                                Sleep (800); //ZzzZzzZzzZzz
                                printf("."); //Information/instructions
                                Sleep (800); //ZzzZzzZzzZzz
                                printf(".\n"); //Information/instructions
                                Sleep (800); //ZzzZzzZzzZzz
                                printf("Thats not an option\n"); //Information/instructions
                                printf("."); //Information/instructions
                                Sleep (800); //ZzzZzzZzzZzz
                                printf("."); //Information/instructions
                                Sleep (800); //ZzzZzzZzzZzz
                                printf(".\n"); //Information/instructions
                                Sleep (800); //ZzzZzzZzzZzz
                                printf("Lets try this again........\n"); //Information/instructions
                                system("pause"); //Press (the) any key to continue
                                system("cls"); //Clear the screen
                                goto top; //Return to "top:"
                                 
                 }
                 










}


Last edited on
This is like some unholy C/C++/nonstandard mashup.

A few tips:

1) Don't use goto. If you want something to loop, put it in a loop.

2) If you find yourself naming variables "answer, answer1, answer2, answer3", you're doing something wrong. Either use an array, recycle variables, or be more descriptive with your variable names

3) printf/scanf are C functions. Since you're already using cout/cint elsewhere, you really shouldn't be using printf/scanf as they have all sorts of type safety issues. And in fact, the reason your program isn't working is because of a type safety issue.

4) char arrays (char fname[20], lname[20]) are dangerous and error prone due to the possibility of buffer overflows. They're also more difficult to use. Again, the reason your program isn't working is related to this. Use std::string instead.

5) main must always return an int. int main()

6) scanf("%c", &junk); //Not sure what this does but it makes things work This goes back to type safety issues and incorrect use of functions you shouldn't be using in the first place. If you find yourself having to do things like this, there is something larger that is going wrong.


All of that said... the problem I found is here:

1
2
3
     scanf("%s",&fname); //Read/store first name
     printf("Please enter your last name:\t"); //Ask user to enter their last name
     scanf("%s",&lname); //Read/store last name 


You're giving the wrong pointer to scanf. You probably meant to be doing this:

1
2
3
     scanf("%s",fname); //Read/store first name
     printf("Please enter your last name:\t"); //Ask user to enter their last name
     scanf("%s",lname); //Read/store last name 


But again... scanf and printf (and char arrays) are not the way to go here. You really should be using cin/cout and strings.

A better solution would be:

1
2
3
4
5
6
7
8
9
10
11
// char fname[20], lname[20]; // get rid of this crap
string fname, lname;  // much better

//...

cout << "Please enter your first name:\t";
getline(cin,fname); //Read/store first name
cout << "Please enter your last name:\t"; //Ask user to enter their last name
getline(cin,lname); //Read/store last name

cout << "Hi " << fname << " " << lname << "\n";


etc
Topic archived. No new replies allowed.