Sign in to follow this  
Narusuke

Simple Help

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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";
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by DevFred
Neither. Prefixing standard names with std:: makes clear you are using a name from the standard library
One purpose is to save you from some typing, otherwise we could have stayed with C-style names (see OpenGL for instance). There is actually not much difference between writing glBegin() and gl::begin().

Though if you are a vendor of code, then namespaces can indeed make your life a bit easier, like this:

namespace foo {
void x () {}
void y () {}
void z () {}
}

instead of
void foo_x() {}
void foo_y() {}
void foo_z() {}


On the gripping hand, if you are not the writer but just a poor maintainer, then the foo_...-convention might actually be easier to read as for the context freenes, or you spread foo::... to make it easier to read again ... Hmm.

Quote:
prevents names clashes.

For that, namespaces do not work better than C-style naming conventions (btw, I hate when vendors publish classes like MySql::MySqlConnection, that's somehow redundant and then, why put it in a namespace at all?). Neither namespaces nor naming conventions, to say nothing of meaningful names, are enforced, nor is it forbidden to inject into stranger namespaces (sometimes it is even encouraged).

Namespaces also have other uses aside from the obvious but in the end non-existant automatic name clash prevention.

Quote:
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.
Note that by using using you do not import all names from another namespace into the top-level namespace, but into the namespace or scope where you use it.

int main () {
{
using namespace std;
cout << "hello" << endl; // okay
}
cout << "fubar"; // nope, out of above scope


{
using std::cout;
cout << "hello" << std::endl; // okay
}

{
using std::cout;
cout << "hello" << endl; // fail, only cout was imported into this scope
}
}



Anyways, I agree that "using namespace" shall never be written in a global context.

[Edited by - phresnel on September 14, 2009 5:38:03 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this