Simple Help

Started by
9 comments, last by phresnel 14 years, 7 months ago
I recently started a C++ class and I wont see the professor for another week, and I'm a bit stuck. We had a home work assignment for the first week of class. It was a fairly easy assignment... or so I thought since I had been reading up on C++ for a while but apparently I don't know as much as I thought I did since I'm stuck on something like this. Anyway here is my program #include <iostream> using namespace std; int main() { char studentName; int studentSocial; int hoursEnrolled; int hourlyTuition; int registrationFee; int insuranceFee; int activityFee; int totalFee; int tuition; cout << "Please Enter Students Name.\n"; cin >> studentName; cout << "Please Enter Student Social.\n"; cin >> studentSocial; cout << "Please Enter Hours Enrolled.\n"; cin >> hoursEnrolled; cout << "Please Enter Hourly Tuition.\n"; cin >> hourlyTuition; cout << "Please Enter Registration Fee.\n"; cin >> registrationFee; cout << "Please Enter Insurance Fee.\n"; cin >> insuranceFee; cout << "Please Enter Activity Fee.\n"; cin >> activityFee; tuition = hoursEnrolled * hourlyTuition; totalFee = tuition + registrationFee + insuranceFee + activityFee; cout << "SSN # -" << studentSocial << "\n"; cout << "Student Name -" << studentName << "\n"; cout << "Total Fee =" << totalFee << "\n"; system ("Pause"); return(0); } It looked good to me, but when I run it, It ask me to enter my name, I enter it and then it displays the first letter only. Then displays the rest of the questions and then waits for me to hit a button and close it, some help would be appreciated. Thanks a lot in advance.
Advertisement
You've defined studentName as a single character. Once that single character has been input, the rest of the input is ignored.

Try:

char studentName[64];

Note: when inputting strings using cin, the first space is considered the end of the string.

Try something like "First Name:", "Last Name:" to reduce the chance of getting a space in the input string.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

This is a quite a common gotcha when starting C++ that can be quite frustrating. std::cin only asks for input from the user when its buffer is empty. For example, since char only holds a single character, inputting "Bob Saget" for studentName extracts the 'B' but leaves "ob Saget" behind. When you attempt to extract a number, studentSocial, from std::cin, std::cin will set itself to a failure state, and will refuse to process any more data until the state has been cleared (and optionally, the input buffer has been cleared).
Wow, I knew it was going to be something really simply I overlooked, I didn't think of that at all. Thanks a lot to the both of you. I will probably be back here a lot over the course of this semester, this book is alright but I think it tries to cover to much at once.
Quote:Original post by Buckeye
Try:

char studentName[64];


Or you do yourself a favour and just use std::string without all the wild guessing about how large a name should be or how long a string somebody might enter.

Quote:Try something like "First Name:", "Last Name:" to reduce the chance of getting a space in the input string.


What if he NEEDS the full name including spaces?

Hint: there are tons of ways to read, read lines, strings, etc. and cin.getline is not the only getline in the library. Contrary to what most references tell you there is also not just a getline taking a char* as destination, but the required overload is most likely declared in the string header.

Reading the entire input and not stumbling over spaces is still just a single line.
f@dzhttp://festini.device-zero.de
This has always worked for me:

#include <iostream>#include <string>int main(){    std::cout << "What is your name? ";    std::string name;    getline(std::cin, name);    std::cout << "Hello, " << name << "!\n";}

I'm not sure if you're allowed to use std::string in your class, but std::string is always much better than char* to represent text. Use "#include <string>" to be able to use it.

A "char" is a single character.
A "char*" is a pointer to a character or a pointer to the first of a whole array of characters, but you need to manually allocate that memory first (typically some fixed size).
A "char[64]" is an array which can contain only up to 64 characters - so longer names will result in a crash
A "std::string" is an automatically memory managed series of characters that can have any size, grow and shrink, without ever needing to worry.

If you would be using C instead of C++, then unfortunately you're stuck with using char* for texts, but since you're using std::cin and std::cout you're clearly using C++ and std::string is the better option.
For this current project char[64] worked fine, but I changed the program around using string and I found I liked that a lot better. Thanks for all the suggestions these will help a lot in the future.


EDIT: Going back I noticed you all used std::cout instead of using namespace std;
Did you just do that to show me in case I didn't know that or do you use std:: cout/cin in higher level programming because of memory cycles?

[Edited by - Narusuke on September 14, 2009 1:04:16 AM]
Moved to For Beginners.
Quote:Original post by Narusuke
I noticed you all used std::cout instead of using namespace std;
Did you just do that to show me in case I didn't know that or do you use std:: cout/cin in higher level programming because of memory cycles?

Neither. Prefixing standard names with std:: makes clear you are using a name from the standard library and prevents names clashes. using namespace std; does the exact opposite of what namespaces were invented for -- it effectively moves all names into the top-level namespace or whatever it's called in C++. I don't see why anyone would ever want to use using namespace std;, except for making code shorter so that more code fits on a page in a book or something.

This topic is closed to new replies.

Advertisement