Re: New to c++.net (need help)
From: Mike Wahler (mkwahler_at_mkwahler.net)
Date: 10/09/04
- Previous message: B. v Ingen Schenau: "Re: New to c++.net (need help)"
- In reply to: Kathyoneal143: "Re: New to c++.net (need help)"
- Next in thread: rossum: "Re: New to c++.net (need help)"
- Reply: rossum: "Re: New to c++.net (need help)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 09 Oct 2004 17:12:50 GMT
"Kathyoneal143" <kathyoneal143@aol.com> wrote in message
news:20041009005350.19078.00002351@mb-m29.aol.com...
> Ok, here is the program. When I put the while statement in there with the
> (again == 'y' || again =='Y')
> cout<<.....
> I run it and it keeps asking me the same question... "Do you want to run
the
> program again" If I type "y" then it keeps asking me the same question but
if I
> type in a number to calculate then it will finish the program again.. Im
sure
> this is something simple and I'm just tired (begining program headaches)
>
> below is the program
>
>
>
> #include <iostream>
> #include <iomanip>
> using namespace std;
>
> int main()
> {
> int numOfEntries,count=1;//Variables for the amount of numbers to enter
and the
> counter
> float total = 0.0;// this is the variable for the total
> char again;// To loop the program
>
> cout <<"This program will calculate a sum of numbers\n";
> cout <<"Please enter how many numbers you want to calculate\n";
> cout <<"The number must be higher than Zero\n";
> cin>>numOfEntries;
> cout <<"\nItem Number\t\tAmount\n";
> cout <<"--------------------------------------\n";
>
> if (numOfEntries <1)
> cout <<"ERROR! You must enter a number greater than zero\n";
>
> while (count <= numOfEntries)
> {
>
> float amountentered;
> cout <<"Amount # " <<count <<"\t\t:";
> cin >>amountentered;
> total +=amountentered;
> count++;
> }
> cout <<fixed<<setw(10)<<setprecision(2);
> cout <<"--------------------------------------\n";
> cout<< "The Total Amount is:\t " <<total <<endl;
> cout<<""<<endl;
>
>
> do
> {
>
> cout << "Would you like to enter more numbers to calculate? (y/n)";
> cin >> again;
>
> } while (again == 'Y' || again == 'y');
>
> cout <<"Please enter how many numbers you want to calculate\n";
> cout <<"The number must be higher than Zero\n";
> cin>>again;
>
> return 0;
> }
Carefully consider the short example I supplied. It indicates
the solution to your problem (but doesn't supply it directly).
Also be sure to look at Bart's reply. Here's a hint for when
your program is behaving other than how you expect, especially
when it involves decision points: Scatter output statements
at stragegic points through your code which simply indicate
that program flow has reached a certain point (this is common
practice especially when one doesn't have or is unable to use
a debugger).
cout << "about to test x\n";
cout << "x == " << x << '\n';
if(x > 42)
{
cout "condition is true\n";
/* your code */
}
else
{
cout "condition is false\n";
/* your code */
}
cout << "after testing x\n";
/* your code */
etc. etc.
Once you've fixed the problems, then go back and remove
or comment out the diagnostic 'cout' statements.
HTH,
-Mike
- Previous message: B. v Ingen Schenau: "Re: New to c++.net (need help)"
- In reply to: Kathyoneal143: "Re: New to c++.net (need help)"
- Next in thread: rossum: "Re: New to c++.net (need help)"
- Reply: rossum: "Re: New to c++.net (need help)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|