C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28691
Number of posts: 94711

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Help with a loop please :) Posted by Ulnarian on 2 Jun 2006 at 10:31 PM
Hey all,

I'm trying a problem in this C book that I picked up and one of them has me stumped. I'm supposed to create a program that uses a loop to sum up monthly sales for the year.

Basically, I want something that asks you, "What were your sales in January", "What were your sales in February", etc., etc.

My thought is something like:

-----------------------------------------
#include <iostream>

int main()
{
using namespace std;

string months[3]={"January", "February", "March"};
int i=0;
int monthly_sales;
int total_sales;

while (i <= 3)
{
cout << "How much did you make in " << months[i] << " : ";
cin >> monthly_sales;
total_sales+=monthly_sales;
++i;
};
cout << "Your total sales are: " << total_sales;

return 0;
}
-------------------------

However, this either crashes, or it returns a crazy value. For instance, I'll enter 1+1+3 for monthly sales and the total sales will be 233243243 or something like that.

Is it wrong to scroll through the elements of a string array the way I've done in this code? If so, what is the proper way to move down the elements of a string array with a loop. Thanks!
Report
Re: Help with a loop please :) Posted by Ulnarian on 3 Jun 2006 at 12:37 AM
Actually, nevermind, I've located the problems :)



: Hey all,
:
: I'm trying a problem in this C book that I picked up and one of them has me stumped. I'm supposed to create a program that uses a loop to sum up monthly sales for the year.
:
: Basically, I want something that asks you, "What were your sales in January", "What were your sales in February", etc., etc.
:
: My thought is something like:
:
: -----------------------------------------
: #include <iostream>
:
: int main()
: {
: using namespace std;
:
: string months[3]={"January", "February", "March"};
: int i=0;
: int monthly_sales;
: int total_sales;
:
: while (i <= 3)
: {
: cout << "How much did you make in " << months[i] << " : ";
: cin >> monthly_sales;
: total_sales+=monthly_sales;
: ++i;
: };
: cout << "Your total sales are: " << total_sales;
:
: return 0;
: }
: -------------------------
:
: However, this either crashes, or it returns a crazy value. For instance, I'll enter 1+1+3 for monthly sales and the total sales will be 233243243 or something like that.
:
: Is it wrong to scroll through the elements of a string array the way I've done in this code? If so, what is the proper way to move down the elements of a string array with a loop. Thanks!
:




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.