|
-
Feb 11th, 2003, 01:36 PM
#1
Thread Starter
Hyperactive Member
Extreme newbie question!!
Hello world (snicker),
Ok I am on the ground floor of learning c++... (no kidding)
I am dabbling around with of course the "Hello World" code and I have used the following;
Code:
#include <iostream.h>
char name;
int main()
{
cout<<"Please enter your name \n";
cin>>name;
cout<<"You entered "<<name;
}
well it WILL ask for the name:
So I put "Anjari"
but the output will only show:
It will not show the full line "Anjari"
what am I doing wrong?
also... If I wanted to add the following:
Code:
#include <iostream.h>
char name;
char accept;
int main()
{
cout<<"Please enter your name \n";
cin>>name;
cout<<"You entered "<<name<<"\n";
cout<<"Was this Correct? (Y/N)";
cin>>accept;
}
It will completely ignore the "Was this Correct? (Y/N)"; part....
why?
Thanks in advance....
Anjari
-
Feb 11th, 2003, 03:38 PM
#2
Member
as for the it showing only an "A" when entering Anjari, its because a "char" only holds 1 character, if u wanted to hold more than one you would either use a char array like this
char myName[9]; //this allocates 10 spaces to put your name into
or u can use strings which are similar
string myName; //in this case u don't need to specify how big it is, it will create it as big as u want it(it has limits but its enough)
to use strings u need to include <string> and use "using namespace std;" or <string.h> and not use "using namespace std;"
and as for ignoring the Y/N, i think its because you didn't set it to do anything if they entered Y or N.
Hope this helps
Death is always smiling down on us, the only thing we can do is smile back
-
Feb 11th, 2003, 03:48 PM
#3
Thread Starter
Hyperactive Member
thanks...
Well I really didnt want the Y/N to do anything but keep the app from shutting down, so I could read the output from the previous area... (Like I said I am a beginner).... But it didnt even ask the question...
So if there is a way to actually "Pause" the program to keep it from closing please let me know..
thanks,
Anjari
-
Feb 11th, 2003, 05:41 PM
#4
Member
yes there is
u put this at the end of the program
system("PAUSE");
i think u have to include <windows.h> to use that
Death is always smiling down on us, the only thing we can do is smile back
-
Feb 12th, 2003, 07:22 PM
#5
You got quite a few things wrong there RabidChimp.
First off, char[9] holds only 8 characters not 10.
Second, you're presenting <string.h> as if it were an option.
Third, it's not <windows.h>, it's <cstdlib>.
Fourth, you have the wrong explanation for the ignoring of the Y/N.
Ok, so let's see.
Code:
#include <iostream> // The .h standard headers are deprecated and therefore evil.
#include <string> // for the string class
#include <cstdlib> // for system()
using namespace std; // don't worry about that now, you'll learn about it soon enough
int main()
{
// global variables (outside of any function) are dangerous and a bad habit to get into
// character arrays are a ready source of errors
// Use the string class for now.
string name;
char accept;
cout<<"Please enter your name" << endl; // Whenever you're finished with a block of text and
// doing something else next (like reading input) you should use endl instead of the last \n.
// You'll learn about that too if your course or book is any good.
cin>>name;
cout<<"You entered "<<name<<"\n";
cout<<"Was this Correct? (Y/N) ";
cin>>accept;
}
As for the Y/N thing: it's a follow-up of the single-character error.
Visualizing what happens in your program should help.
cout<<"Please enter your name \n";
This requests cout to write that string to the screen. But cout doesn't do that. In order to be more efficient, cout collects a bunch of characters in a buffer and then outputs them all at once. The entire string fits into the buffer so cout doesn't write it to the screen just yet.
You can force cout to write the buffer to the screen anyway by flushing it. That's what endl does btw: it outputs a newline and then flushes the buffer.
cin>>name;
cin is asked for a character. cin looks in its own (input) buffer and sees that there are no characters. It therefore enters wait state, which also means that it flushes the associated output buffer: the string sent to cout is only now written to the screen. You enter "Anjari" and hit enter. The enter causes cin to wake up and check the buffer again. Voilá, there is an 'A' waiting. cin returns the A.
Next, cout is again requested to output something. Again it all disappears in the buffer.
Then cin is asked for a second character. But this time it doesn't have to wait: there is still the 'n' from "Anjari" waiting.
So the output buffer doesn't get flushed and the string never appears. Neither does cin wait for input. The program ends and you're left in the dark.
I hope this was a good explanation, if not feel free to say so.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 12th, 2003, 08:25 PM
#6
Junior Member
char[9] will hold 10 characters. It starts from 0 and works it way up till 9. 0 1 2 3 4 5 6 7 8 9 is 10. You could also use getch(); to pause the program.
#include <conio.h>
#include other libraries
int main(void)
{
your code here
getch(); // to hold the screen
return 0;
}
-
Feb 12th, 2003, 08:46 PM
#7
char[9] holds 9 characters, actually. You're correct in that the index starts at 0, but it doesn't go up to 9, it goes up to 8. 9 is the number of elements, not the upper bound. but CornedBee is right because the 9th (should) always be a null character if you're dealing with strings.
Code:
char[9] myChars;
myChars[0] = 'a'; // first element
myChars[8] = 'z'; // last element
myChars[9] = '?'; // out of bounds. dont do this.
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Feb 13th, 2003, 10:50 AM
#8
Thread Starter
Hyperactive Member
Thanks
Thank you everyone for you input.. it is greatly appreciated... Without you guys I don't know where I would stand as far as learning...
Thanks again,
Anjari
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|