Help I'm stuck!

Hi I'm new to C++, and I'm having an issue with this code where I need to enter information on each of the lines; however for some reason I can only enter information for "My name is". once I enter my name it goes to the end of the program, and won't let me enter anything else. What am I doing wrong? thanks in advance for the help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  #include <iostream>
using namespace std;
int main()
{
	double name, major, creditHour, perHour, total;

	cout << "\nMy name is ";

	cin >> name;

	cout << "\nI am majoring in";

	cin  >>  major;

	cout << "\nI am taking"     << "credit hours\n";

	cin >> creditHour;

	cout << "\nI am paying $" << "per credit hour\n";

	cin >> perHour;

	cout << "\nTotal amount this term is $";

	total = creditHour * perHour;

	cout << endl << endl;
	system("pause");
	return(0);

}
I just want to point out to you that... I don't think name and major are doubles ;)
Ah ok, thanks! I fixed the first two lines with that hint!
Does it work now?
Change name and major to std::string

1
2
double creditHour, perHour, total;
string name, major;
// don't forget to #include <string>

I would also like to mention that you're not printing out the "total amount this term".

1
2
total = creditHour * perHour;
cout  << "\nTotal amount this term is $" << total;


Since you're new to c++, it's much better to get into good habbits early. One of them is using std:: instead of using namespace std; For example -
1
2
3
4
5
6
7
8
9
double name, major, creditHour, perHour, total;

	std::cout << "\nMy name is ";

	std::cin >> name;

	std::cout << "\nI am majoring in";

	std::cin  >>  major;


You can read plenty of that here - http://bfy.tw/3u3L
Last edited on
@Tarik

Is it that important to drop using namespace std; and using std::? I'm obtaining my bachelors in comp sci and none of my professors have stressed the importance of std:: over just using namespace std; (maybe i had bad professors)
Non of my professors stressed the importance of std:: either. Beginners are usually taught to just use using namespace std; and not worry about it. But yes it's important, it'll save you lots of headache later on.
I'm obtaining my bachelors in comp sci and none of my professors have stressed the importance of std:: over just using namespace std;


To some extent, this is to be expected. Computer Science is not programming. A programming language is a convenient notation to use to express and demonstrate some computer science concepts, and it's nice to know how to use that notation in a professional software development context, but CS isn't a "how to program" apprenticeship - which is fine, and how it should be. You just need to remember that your degree isn't about turning you into a professional programmer, and that if that's your aim, you're going to have to work on the craft of programming independently.
Last edited on
Topic archived. No new replies allowed.