program window closes to quick to view any output

Hello, I know there has been topics on this before and I have read them through, and tried the suggestions that people have made, but for whatever reason I can't get them to work when I try it. I have tried adding system("PAUSE"), getchar(), _getchar(), and std::cin >>. I don't know if its the compiler that I am using (Devc++) or what, but nothing seems to ever work. As my name suggest, I am totally new at this, so if you could be as specific as possible, such as where exactly to insert anything that I am missing, I would trully appreciate it.

Thanks!

Here is an example of a sample program I am trying to view...

#include <stdio.h>

int main()
{
char name[20];
char color[20];

printf("What is your name?");
scanf("%s", name);
printf("What is your favorite color?");
scanf("%s", color);
printf("%s's favorite color is %s\n",name,color);

return(0);

}
closed account (Lv0f92yv)
You could try a cin.ignore();, then a getchar(); call BEFORE the return statement of your program. If you're program 'returns' before any of those calls, it won't matter what you put there.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>

int main()
{
char name[20];
char color[20];

printf("What is your name?");
scanf("%s", name);
printf("What is your favorite color?");
scanf("%s", color);
printf("%s's favorite color is %s\n",name,color);

cin.ignore();
getchar();
getchar();
return(0);

}


I would expect this to work.
See the sticky "Console Closing Down".
Thanks Desh,

I tried doing what you recommended and it didn't work. Here is what I wrote out.

#include <stdio.h>

int main()
{
char name[20];
char color[20];

printf("What is your name?");
scanf("%s", name);
printf("What is your favorite color?");
scanf("%s", color);
printf("%s's favorite color is %s\n",name,color);

cin.ignore();
getchar();
getchar();
return(0);

}


And this was the error message that I got.

In function 'int main()'
13 'cin' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)

I'm not exactly sure what it means.
Hi Sirnoobalot,

First suggesstion, you should write the Code in code tags. It makes it easier for other to read your code.

As for your problem, you should try std::cin.ignore();.

Try the following


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
#include <iostream>

int main()
{
	char name[20];	
	char color[20];

	printf("What is your name?\n");
	scanf("%s", name);
	printf("What is your favorite color?\n");
	scanf("%s", color);
	printf("%s's favorite color is %s\n",name,color);

	getchar();
	return(0);
}
Then that makes the iostream include redundant.

Ideally, what you'd do is
1
2
#include <limits>
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );


-Albatross
Thanks for the knowledge everyone. I appreciate it. I was able to get the program to stop.....sorta. The getchar() function seems to work sometimes, but not all the time. I'm curious why this is. When I added it in to the original program I posted about, I couldn't get the program to pause. However when I reached another exercise in the book that I'm reading, I threw it in there just before the return(0) function and it worked.

I'm baffled why it works for one and not the other????

Here is the program that I was able to pause...

#include <stdio.h>

int main()
{
char jerk[20];

printf("Name some jerk you know:");
gets(jerk);
printf("Yeah, I think %s is a jerk, too.\n",jerk);
getchar();
return(0);
}
I wonder why people are using gets() when scanf() is so much safer.

Oh, and it's because scanf leaves the newline in the stream, which getchar() reads.

-Albatross
Last edited on
Topic archived. No new replies allowed.