Connect Input and Output



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

#include<iostream>

#include<cmath>

#include<ctime> 

using namespace std;

int main () {

	int tDate; int tMonth; int tYear; 
	int bDate; int bMonth; int bYear;  
	int hour; int min; int sec;       
	int resultDate, resultMonth, resultYear; 
	
	
	time_t result;
	result = time(NULL); 
	struct tm* tp = localtime(&result); 

	tMonth = tp->tm_mon + 1;   
	tDate  = tp->tm_mday;  
	tYear  = tp->tm_year + 1900; 
	hour   = tp->tm_hour;
	min	   = tp->tm_min;
	sec    = tp->tm_sec;
	
	

	
	cout << "Please Enter your birthday: "; 
	cin >> bMonth; 
	cout << "/";
	cin >> bDate ;
	cout << "/";
	cin >> bYear;
	


	

	resultDate  = tDate  - bDate;
	resultMonth = tMonth - bMonth;
	resultYear  = tYear  - bYear;

	
	cout << "Your age is: " << resultYear << " year(s) " 
		 << resultMonth << " month(s) and " 
		 << resultDate << " day(s)." << endl 
		 << hour << " hour(s) "
	     << min   << " min(s) "
		 << sec   << " second(s)."<< endl;
	return 0;

}






I WANT TO CONNECT

cout << "Please Enter your birthday: ";
cin >> bMonth;
cout << "/";
cin >> bDate ;
cout << "/";
cin >> bYear;

IN ONE LINE LIKE THIS

Please Enter your birthday: 11/15/1990

Help me please. Thanks
How familiar with <string> are you? If the answer is not very familiar, then go and do some basic examples in it first.
I just want to automatic add slash after the input
In other words, you want to control what the terminal shows while you type?
Sample is when i type the month

Please Enter your birthday: 9

it will auto put slash "/"

and you input again the next the day

Please Enter your birthday: 9/5

it auto put slash again and then enter the last


Please Enter your birthday: 9/5/1990

like this
I misunderstood your question... You just need to put std::cout << "/" after the user input.

Misunderstood again... lol I can't actually see a way of making a program do what you want. If I type '3' it can't put a slash in case I'm about to put a '0' or '1' after the 3... But if my birthday is just on the third, how can the program know?
Last edited on
the problem is a newline is automatically added after cin >>
thus the slash character is appended below the input:

try to look at this:
http://stackoverflow.com/questions/15209370/how-do-i-input-variables-using-cin-without-creating-a-new-line
ncurses or similar library offers character-based "graphical" UI features, so it could allow an "edit box", where the user can move cursor/focus to and enter text, maybe even validate that it is an integer in valid range.

Regular ANSI terminal control codes could possibly backtrack the newline too, in order to return the cursor into desired position before output of slash character.
assuming your on windows, i made this little gotoxy() clone:

i'm new to this so i don't know if there is a more efficient way :

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
#include <iostream>
#include <windows.h>

void gotoxy( short x, short y )
{
    static HANDLE hstdOut = GetStdHandle( STD_OUTPUT_HANDLE );
    COORD coord = { x, y };
    
    SetConsoleCursorPosition( hstdOut, coord );
}

int main ()
{
    using namespace std;
    
    int month, day, year;

    system( "cls" ); // for demonstration purposes only

    cout << "Enter your birthday: ";
    cin >> month;
    gotoxy( 23, 0 ); cout << " / ";
    cin >> day;
    gotoxy( 29, 0 ); cout << " / ";
    cin >> year;
}


Sample output:
Enter your birthday: 04 / 28  / 1997
Last edited on
Topic archived. No new replies allowed.