Click to See Complete Forum and Search --> : simple C++ problem


rimby
November 3rd, 2001, 07:08 PM
I can't figure this out. What am I missing in this simple little program? I can build & compile this and there are no errors or warnings. But as soon as I enter a string and press enter everthing goes away.

#include <iostream>

using namespace std;

int main()
{

char s[80];

cout << "Enter a character string" << endl;
cin >> s;
cout << "The string you entered is: " << s << endl;

return 0;
}

Andreas Masur
November 3rd, 2001, 07:19 PM
Hi,

There is nothing wrong with your code. The only problem I see will occur if you write over the boundaries which means typing in more than 80 characters...but besides that the program will work fine...

Did you debug your code?? If you still have the problem it would be useful to see some debug output like the function its crashes etc.

Ciao, Andreas

"Software is like sex, it's better when it's free." - Linus Torvalds

rimby
November 3rd, 2001, 07:38 PM
Thank you for replying. I was able to compile and run my program and it does display the first cout statement. I get to type in a string of characters and once I press enter the screen disappears. I wish there was a way to pause after I type in a string and press enter so that my program will go to the 2nd cout statement. I hope this makes sense.

Thanks Rimby.

Paul McKenzie
November 3rd, 2001, 09:12 PM
I compiled and ran your program. There is nothing wrong with entering characters until you enter a space.

If you enter a string with spaces, operator >> will stop at the first space. Use getline() if you want to make sure that the entire string is read in:

cin.getline(s,79);



Regards,

Paul McKenzie

rimby
November 3rd, 2001, 10:44 PM
Thank you so much for replying. I made that change. It compiled w/out errors or warnings but as soon as I typed in a string of characters and then pressed enter, the box or screen went away. Perhaps it is a software issue.
I really like your suggestion!
Thanks Rimby

James Curran
November 3rd, 2001, 11:21 PM
You are writing this under Windows, correct? If so, there's nothing wrong with your program --- it's doing exactly what you told it to do.

When you run a "Console-mode" program like this under Windows, a "console window" is created just for it. Once the program ends, the windows is destroyed. So, after you enter your text, the program print the line in the window, and then the programs ends, the window closes. It probably happened so fast that you didn't see it print the line.

If you open a Dos Session manually, and then run the program by typing it's name at the prompt, the window should stay open.

Truth,
James
http://www.NJTheater.com
http://www.NovelTheory.com
I don't do it for the points (OK, maybe I do), but rating a post is a good way for me to know if I helped.

rimby
November 4th, 2001, 12:06 AM
Thank you so much, I was going insane trying to figure out why? Now I can rest about it. Your tip has been a great help.
Thanks again,
Rimby

Andreas Masur
November 4th, 2001, 12:08 PM
Hi,

Your second 'cout' will be executed but since you exit your application right after printing the second line via the 'return' statement you will probably not see the output very well. You can add a function like 'getchar()' (in Windows you can use '_getch()', defined in conio.h) which is defined in stdio.h. It will wait for another keystroke. So

#include <iostream>
#include <stdio.h> // in Windows use conio.h

using namespace std;

int main()
{
char s[80];
cout << "Enter a character string" << endl;
cin >> s;
cout << "The string you entered is: " << s << endl;

getchar(); // in Windows use _getch()

return 0;
}



The way James described works of course as well...

Ciao, Andreas

"Software is like sex, it's better when it's free." - Linus Torvalds

rimby
November 4th, 2001, 03:01 PM
I like both ideas. I tried your idea just now and that window still disappears. I can watch it by stepping into and over the function and it works. I just would really like it, if the window would just stay after I enter my string of characters w/out spaces.

I will keep trying!

Thanks you again,
Rimby

Andreas Masur
November 4th, 2001, 11:09 PM
Hmmm, well my description was not totally clear. 'getchar()' and '_getch()' don't wait for the next keystroke. It's just not the proper description but I was a little bit in hurry. Both functions are reading one character from the input stream in your case 'stdin' (Standard input). Normally you can use them to wait for one keystroke as well. BUT, if some character is still waiting in the input streambuffer this function won't wait, it will immediately read the next character and in your case close the window.

As Paul already mentioned, if you type in some kind of string with a space, operator '>>' will stop at the space. So the rest of your string will stay in the input streambuffer and if you call 'getchar()' or '_getch()' it will notice that there are characters waiting inside the buffer and will read the first character from it. If you type in

"This is a string"

's' will only contain "This ". If you then call 'getchar()' or '_getch()' they will return 'i' since this is the next character which is still inside the buffer.

That might be the problem, I don't know if your program is running under Windows but normally '_getch()' shouldn't make bigger problems.

Ciao, Andreas

"Software is like sex, it's better when it's free." - Linus Torvalds

Oliver Wraight
November 5th, 2001, 05:59 AM
Try pressing <Ctrl> + F5 instead of just F5 when you run the program. This will make Visual Studios ask for a keypress before closing the console window when the program exits.

Hope this helps,

Oliver

rimby
November 10th, 2001, 11:07 PM
Hello,

Someone suggested to me to use:

system ("PAUSE");

prior to the return and it worked perfectly. Someone else suggested using "ignore" but I wasn't sure how to do this.

Rosee