C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

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

Report
New to programming, this should be an easy one! Posted by Pepperlizard on 9 Sept 2012 at 2:18 AM
I'm just starting to learn programming, and I ran in to a problem very early in my program. The first thing that I prompt the user for is the month, and that works fine. However it doesn't keep working after that, instead of just asking the user for the number of cookies made, it outputs the messages asking for the next 3 variables and then completes, so no other variables are entered. I'm stumped and I'm hoping that you can help me to get over this first obsticle so that I can continue working on my program.

code:

#include <iostream>
using namespace std;

int main()
{
  int month;
  int cook_made;
  int reg_price;
  int dis_price;

  cout << "Enter the first three characters of the month:  ";
  cin >> month;
  cout << "Enter the number of cookies made:  ";
  cin >> cook_made;
  cout << "Enter the regular price for a box of cookies:  ";
  cin >> reg_price;
  cout << "Enter the discount price for a box of cookies:  ";
  cin >> dis_price;

  return (0);
}


and what I get when I run the program:

bash-3.00$ ./a.out
Enter the first three characters of the month: Dec
Enter the number of cookies made: Enter the regular price for a box of cookies: Enter the discount price for a box of cookies: bash-3.00$
Report
Re: New to programming, this should be an easy one! Posted by formitzbsd on 18 Sept 2012 at 4:03 PM
The reason your program is exiting is because you are asking for the first 3 letters of a month. You declared month as a type int and that will not accept characters. You need to change

int month;

to

string month;

and it will work as intended.
Report
Re: New to programming, this should be an easy one! Posted by formitzbsd on 18 Sept 2012 at 4:04 PM
The reason your program is exiting is because you are asking for the first 3 letters of a month. You declared month as a type int and that will not accept characters. You need to change

int month;

to

string month;

and it will work as intended.
Report
Re: New to programming, this should be an easy one! Posted by ahmedcab on 5 Oct 2012 at 2:05 PM
your problem is that you use the int type for month when you should use the string type or the char array type
i have a tutorial on data types in my youtube channel
http://www.youtube.com/watch?v=X1lDaCt2jCM&feature=plcp
it will show you the difference between the type
ps: i'm not a native english speaker so my pronuncation is not very good.
Report
Re: New to programming, this should be an easy one! Posted by ahmedcab on 5 Oct 2012 at 2:07 PM
your problem is that you use the int type for month when you should use the string type or the char array type
i have a tutorial on data types in my youtube channel
http://www.youtube.com/watch?v=X1lDaCt2jCM&feature=plcp
it will show you the difference between the type
ps: i'm not a native english speaker so my pronuncation is not very good.



 

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.