![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2005
Posts: 5
Rep Power: 0
![]() |
Trouble with a swith statement
so i was given an assignment to make a calculator in c. i needed two call functions, one to get get information from the user, and one to do the next operation. the first function consist of a sole scanf statement and gets both the operator and right operand. the second function is a switch statement to do the next operation.
my problem is that the project stipulations state that the starting number must be zero, meaning the first input should consist of an opertor and operand. thus, the user would put +5 and the calculator should 0+5. the following operation should be, for example, input:+5 answer 10. for some reason i cant get it to do this. instead any input after the first try gives an answer of 0. the reason i beleive there is a problem with the switch is that when i try to q (input q) i have to do it twice. the first time i put in q i get an answer of 0 and am returned to the program, in order to quit, i must enter q a second time. i know this is probably more confusing than anything else, but i was wondering if there is a common problem with switch statements that would cause this. i wont put my code here because theres 90 lines of it, and im not sure what part you need to see. i would appreciate any help. thanks. |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
The only common problem with switches i know is that people often forget to terminate every case with a break:
switch(i) {
case 1:
. . .
break; // if you do not put the break statement here, the switch will continue in case 2.
case 2:
. . .
} |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Sep 2005
Location: GA
Posts: 99
Rep Power: 4
![]() |
I think a switch is like a while loop. that why it require a break. it execute the block of code as long as the case is true.
|
|
|
|
|
|
#4 | |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Sep 2005
Location: serbia & montenegro
Posts: 484
Rep Power: 4
![]() |
A switch it's more like many connected if-elseif constructs, but that's only me...
|
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
If you post your code, we don't have to guess about what might be wrong.
|
|
|
|
|
|
#7 | |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#8 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
this sounds like basic administrative stuff. there are many possible causes for your problems, all minor logic errors or syntax errors. we need to see the code.
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
|
|
#9 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
|
#10 |
|
Programming Guru
![]() |
Erm you dont need a break in every statement in a switch, only if you want it to execute one section.
The way a switch statement works is by going down looks for the first case it meets then keep flowing down running all the code below it until it hits a break which will cause it to exit.
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity." - Albert Einstein |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|