"Supposedly" easy program not working... With no errors

May 8, 2009 at 4:50pm
The program is as follows:

Write a program that uses four functions to do the following:
**USE ALL GLOBAL VARIABLES

1.) Ask the user for his name, age, and birthday

2.) Print his name 20 times using a DO WHILE loop

3.) Print his name, age, and birthday in the center of the screen for 5 sec.

4.) Tell the user if he is older, younger, or the same age as yourself


Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <conio.h>

using namespace std;

const int NotUsed = system( "color 0a" );

void UserInfo();
void NameLoop();
void PrintCenterInfo();
void OlderYounger();

char name[20];
char birthday[20];
int age, MyAge = 17, x = 1;

int main()
{
	UserInfo();
		cout << '\n';

	NameLoop();
		cout << '\n';

	PrintCenterInfo();
                cout << '\n';

	OlderYounger();
		cout << '\n';

	return 0;
}

void UserInfo()
{
	cout << "Enter your name: ";
	cin.get(name, 20);
	cout << "Enter your age: ";
	cin >> age;
	cout << "Enter your birthday: ";
	cin >> birthday;
}

void NameLoop()
{
	do
	{
		x++;
		cout << name << '\n';	
	}
	while(x != 21);
	system("PAUSE");
}

void PrintCenterInfo()
{
        system("CLS");
	cout << name << '\n';
        cout << age << '\n';
        cout << birthday;
	Sleep(5000);
}

void OlderYounger()
{
	if(age < MyAge)
	{
		cout << "You are younger than me.";
	}
	else if(age > MyAge)
	{
		cout << "You are older than me.";
	}
	else if(age == MyAge)
	{
		cout << "You are the same age as me.";
	}
}



For some reason, it just ends at the end of the function PrintCenterInfo(). It
sleeps for 5 seconds, but it never goes back to the main() function, so it never calls OlderYounger(), or print a new line '\n'.

I tried commenting Sleep(5000); out of the program, but it does the same thing.

Can someone please help me out?
May 8, 2009 at 4:57pm
The problem could be that your console window won't stay open after OlderYounger() so you won't read the output.
If this is the problem check this out: http://www.cplusplus.com/forum/articles/7312/
May 8, 2009 at 5:01pm
Thanks Bazzy.

I checked the topic out, but there's still one thing I am confused about:

How do I know that's the problem?

And if it is the problem, where would I include any of that?

Again, thank you soo much for your help.
May 8, 2009 at 5:10pm
If that is the problem use cin.ignore( numeric_limits<streamsize>::max(), '\n' ); at the end of your program ( just before return 0; in main )
May 8, 2009 at 5:24pm
A slight problem to this is that my teacher has no idea how to use c++... Let alone how to program.

So I don't really understand

cin.ignore( numeric_limits<streamsize>::max(), '\n' );

what that does...
May 8, 2009 at 5:56pm
It is explained in the link I posted earlier: it makes the console window stay open until the user presses enter
Topic archived. No new replies allowed.