Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 7th, 2005, 1:23 PM   #1
cjaime
Newbie
 
Join Date: Nov 2005
Posts: 5
Rep Power: 0 cjaime is on a distinguished road
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.
cjaime is offline   Reply With Quote
Old Nov 7th, 2005, 1:26 PM   #2
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
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:
  . . .
}
Polyphemus_ is offline   Reply With Quote
Old Nov 7th, 2005, 2:04 PM   #3
Master
Programmer
 
Master's Avatar
 
Join Date: Sep 2005
Location: GA
Posts: 99
Rep Power: 4 Master is on a distinguished road
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.
Master is offline   Reply With Quote
Old Nov 7th, 2005, 2:26 PM   #4
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
Quote:
Originally Posted by Master
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.
Wrong. It doesn't stop executing code after a case with the right value, unless you break.
Polyphemus_ is offline   Reply With Quote
Old Nov 7th, 2005, 2:40 PM   #5
ivan
Professional Programmer
 
ivan's Avatar
 
Join Date: Sep 2005
Location: serbia & montenegro
Posts: 484
Rep Power: 4 ivan is on a distinguished road
A switch it's more like many connected if-elseif constructs, but that's only me...
ivan is offline   Reply With Quote
Old Nov 7th, 2005, 3:44 PM   #6
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
If you post your code, we don't have to guess about what might be wrong.
jim mcnamara is offline   Reply With Quote
Old Nov 7th, 2005, 4:06 PM   #7
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
Quote:
Originally Posted by ivan
A switch it's more like many connected if-elseif constructs, but that's only me...
I believe it uses less stack than if-else if constructs.
Polyphemus_ is offline   Reply With Quote
Old Nov 8th, 2005, 12:28 AM   #8
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
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.
bl00dninja is offline   Reply With Quote
Old Nov 8th, 2005, 6:29 AM   #9
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by ivan
A switch it's more like many connected if-elseif constructs, but that's only me...
Mind that switch needs constant cases to work. OP, like others said: post the code.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Nov 8th, 2005, 7:36 AM   #10
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
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
Berto is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:59 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC