C++ programmers, help a newb!

RaynorWolfcastle

Diamond Member
Feb 8, 2001
8,968
16
81
What is the command to pause the system until the user presses a key?
I only need this at the end of my (very simple) program. VS 6 used to do it alone, but VS.NET doesn't.

I've used the getch() function, but for some reason, it screws with my cout statements.

Can you please help?

-Ice
 

Sir Fredrick

Guest
Oct 14, 1999
4,375
0
0


<< why dont you just
string dumb;
cin>>dumb;
>>



because then the user has to press return. Unfortunately, I don't know the answer to this.
 

RaynorWolfcastle

Diamond Member
Feb 8, 2001
8,968
16
81
I could do that, but it seems so unelegant (is that a word?) and the user has to hit enter. I want something that will just pause until hits any key.

Side question, is there any way to get a cout.width statement to last for more than 1 cout?

-Ice
 

RSMemphis

Golden Member
Oct 6, 2001
1,521
0
0
getchar();

should work so that you only need to press a key.
As with all C functions, you can ignore the return value (a char).
 

RaynorWolfcastle

Diamond Member
Feb 8, 2001
8,968
16
81


<< getchar();

should work so that you only need to press a key.
As with all C functions, you can ignore the return value (a char).
>>



getchar() gives me an undeclared indentifier error, which header file do I need to include?

Is getchar() the same as getch() ? because getch() causes problems with my cout statements (don't ask me why)

-Ice
 

RSMemphis

Golden Member
Oct 6, 2001
1,521
0
0
#include <stdio.h>

Sorry, it's a standard line in my programs, forgot to give it to you.

Edit: getch and getchar are two different commands, and only getchar is really supported in VS6 and VS.NET
 

beer

Lifer
Jun 27, 2000
11,169
1
0
Try the cin.ignore() function of the <iostream.h> library.

It should work. Getchar() usually doesn't.
 

RaynorWolfcastle

Diamond Member
Feb 8, 2001
8,968
16
81
I am incredibly stumped, I see the things appear, AFTER the commands but the getchar() or cin.ignore() are the last things in main. :confused: :confused: :confused:
*edit* using 2 cin.ignores works but there is nothing between them and I only have to hit enter once :confused:

-Ice
 

Sir Fredrick

Guest
Oct 14, 1999
4,375
0
0


<< I am incredibly stumped, I see the things appear, AFTER the commands but the getchar() or cin.ignore() are the last things in main. :confused: :confused: :confused:
*edit* using 2 cin.ignores works but there is nothing between them and I only have to hit enter once :confused:

-Ice
>>



I might have an idea what you're doing wrong. cin is getting messed up because it's not pulling the newline character at some point. Post your code and I might be able to point out where. You may have to use a cin.clear() (I think that's the function, don't quote me on it).
 

notfred

Lifer
Feb 12, 2001
38,241
4
0


<< #include "windows.h" (i think)

system("PAUSE")
>>



This works well, but displays "Preass any key to continue..." on the screen. I don't think it's in windows.h though.
 

ThaGrandCow

Diamond Member
Dec 27, 2001
7,956
2
0


<<

<< #include "windows.h" (i think)

system("PAUSE")
>>



This works well, but displays "Preass any key to continue..." on the screen. I don't think it's in windows.h though.
>>

Why wouldn't you want the "Press any key to continue..." message? If it doesn't show up the person could sit there waiting for teh program to end.

Chris: Dump the intro to your site. It's pretty ghey (all intros are, not singling you out)
 

Sir Fredrick

Guest
Oct 14, 1999
4,375
0
0


<<

<< #include "windows.h" (i think)

system("PAUSE")
>>



This works well, but displays "Preass any key to continue..." on the screen. I don't think it's in windows.h though.
>>



You need to include <process.h> or <stdlib.h> to use it.
 

RaynorWolfcastle

Diamond Member
Feb 8, 2001
8,968
16
81
Well, I went to see my prof today and he explained what the problem was. Apparently it has something to do with the way cin works. He explained to me how to use cin.clear() to remedy the solution. Thanks to all that offered help. I fixed the link in case any of you want to see what the source file looked like anyway.

-Ice
 

RaynorWolfcastle

Diamond Member
Feb 8, 2001
8,968
16
81


<< Chris: Dump the intro to your site. It's pretty ghey (all intros are, not singling you out) >>



Yeah, I'm in the process of updating my site, that's one thing I'm getting rid of. Well not quite, I'm keeping it somewhere insde the site to show that I have some Flash skills. But it's gone from the front page. :)

-Ice
 

Sir Fredrick

Guest
Oct 14, 1999
4,375
0
0


<< Well, I went to see my prof today and he explained what the problem was. Apparently it has something to do with the way cin works. He explained to me how to use cin.clear() to remedy the solution. >>



Told ya. ;) Glad you got it working. The code wasn't that hard to understand despite the lack of comments :)