![]() |
|
#1
|
|||
|
|||
Program won't work in 1 instanceHey, I've written a program for my class that takes a binomial equation i.e. (ax + by)^n, and computes a solution. The user uses that syntax, but only specifies a (x coefficient), b (y coefficient), and n (exponent). The program works in all instances EXCEPT when a is entered as '0'.
so, for example: them entering (0x + 3y)^5 sends my program into an endless loop. I use error checking code but it doesn't have an effect. what baffels me, is that the error checking code catches if b (the y coefficient) is entered as zero. so (2x + 0y)^3 does work. All input is taken in order, so it goes: char >> int >> char >> char >> int >> char >> char >>char >> int And the error checking code (in a nutshell) is: if (a != 0 && b != 0) is_err = false; else is_err = true; Thanks for looking... Any insights? |
|
#2
|
||||
|
||||
|
Try this instead
CPP / C++ / C Code:
I think the precedence is evaluating the && before the comparisons. |
|
#3
|
|||
|
|||
|
Thanks for the reply, but it didn't make a difference.
I think the problem lies within the collection of the data (maybe using the cin function), but I don't know how else to go about solving the problem. I know the error checking shouldn't matter because before I checked if b was zero, the program still solved the equation. So the reason the program is crashing shouldn't be because the error handling is wrong. Any other ideas guys? |
|
#4
|
|||
|
|||
|
Bizarre. Have you tried splitting it up, i.i if(a!=0) blah; if(b!=0) blah?
GF |
|
#5
|
||||
|
||||
|
Also, Davey. If you are not sure that your input method is working, why don't you print an input summary before proceeding with your process? You could take this out later, but for debugging, it is really useful.
I think I get what you are doing with your input function of cin and it is pretty inovative. You may run into problems when others run your program though, because they may space or not space. It is cool to be able to enter a string like this, but have you thought about doing something more straightforward: CPP / C++ / C Code:
Not nearly as elegant, but alot easier to get input and check for errors... |
|
#6
|
|||
|
|||
|
Yeah, GF, I tried every which way reguarding if statements, but nothing seems to be effective. dsmith, I would love to be more straight forward, like you said, and I'm sure it would work that way, but that isn't my asignment.
I think what I'm going to do is get the input into a string with getline() and separate all the necessesary things there. I'll let you know if I get the thing going. Thanks for your help |
|
#7
|
||||
|
||||
|
i get what u all means
cannot use if (a != 0 && b != 0) because when u key in data not all the time a & b are = 0 some time is a =0 but b!=0 or b=0 but a!=0 so u can use this if (a!=0 || b!=0) instead of this u also can u set do... while() condition between u cin<<char<<int......... e.g do{ ....... cin<<char<<int<<......... ....... }while(a!=0 || b!=0) // once user key in 0 either a or b or both will gv error this do.. while() make user re key in the data again if they type wrong |
Recent GIDBlog
Stupid Management Policies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| A question about the Amazon Affiliate Program | JdS | Advertising & Affiliates Forum | 5 | 13-Jan-2004 18:00 |
| error during program | rjd72285 | C++ Forum | 0 | 11-Nov-2003 19:49 |
| Why doesnt my form work correctly? | rhino1616 | Web Design Forum | 2 | 06-Nov-2003 18:21 |
| How do web redirection scripts work? | rhino1616 | Web Design Forum | 9 | 27-Oct-2003 10:47 |
| one program access another? | dgoulston | C++ Forum | 1 | 07-Oct-2003 12:26 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The