GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-Jan-2004, 21:41
daveyp225 daveyp225 is offline
New Member
 
Join Date: Oct 2003
Posts: 4
daveyp225 is on a distinguished road
Question

Program won't work in 1 instance


Hey, 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  
Old 28-Jan-2004, 21:57
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Try this instead

CPP / C++ / C Code:
if ( (a != 0) &&  (b != 0) )

I think the precedence is evaluating the && before the comparisons.
  #3  
Old 28-Jan-2004, 23:05
daveyp225 daveyp225 is offline
New Member
 
Join Date: Oct 2003
Posts: 4
daveyp225 is on a distinguished road
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  
Old 29-Jan-2004, 08:52
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
Bizarre. Have you tried splitting it up, i.i if(a!=0) blah; if(b!=0) blah?
GF
  #5  
Old 29-Jan-2004, 09:10
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
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:
cout << "Enter the following variables for a,b and n such that (ax+by)^n\n";
cout << "Enter a: ";
cin >> a;
cout << "Enter b: ";
cin >> b;
cout << "Enter c: ";
ciin >> c;

Not nearly as elegant, but alot easier to get input and check for errors...
  #6  
Old 29-Jan-2004, 12:47
daveyp225 daveyp225 is offline
New Member
 
Join Date: Oct 2003
Posts: 4
daveyp225 is on a distinguished road
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  
Old 04-Feb-2004, 15:09
tay's Avatar
tay tay is offline
Junior Member
 
Join Date: Jan 2004
Posts: 77
tay will become famous soon enough
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 GIDBlogStupid Management Policies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

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

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

All times are GMT -6. The time now is 06:30.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.