Writing dates with strings

Not sure why character won't print

Page 1 of 1

14 Replies - 799 Views - Last Post: 20 February 2009 - 03:13 PM Rate Topic: -----

#1 Saige89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-February 09

Writing dates with strings

Post icon  Posted 19 February 2009 - 01:50 PM

well i have a code that i need to input some dates with strings but i'm not sure why the char slash won't appear unless i put it

 #include <iostream>
#include <string>
using namespace std;

int main()
{
	int m;
	int d;
	int y;
	
	cout << "enter month" << endl;
	cin >> m; 
	char slash;
	cin >> slash;
	cout << "enter day" << endl;
	cin >> d;
	cin >> slash;
	cout << "enter year" << endl;
	cin >> y;
	string activity;
		getline(cin, activity);
	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Writing dates with strings

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Writing dates with strings

Posted 19 February 2009 - 02:08 PM

In programming, nothing appears unless the programmer makes it happen. Maybe I'm missing the point? I don't really understand what you're asking. You could cin each variable and then add slashes manually later. alternately you could make a Date class.
Was This Post Helpful? 0
  • +
  • -

#3 Saige89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-February 09

Re: Writing dates with strings

Posted 20 February 2009 - 11:50 AM

well i want the character slash to appear but it won't so i put string in the code it see if it would work...that's why i put getline because it can use string characters
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Writing dates with strings

Posted 20 February 2009 - 11:58 AM

char slash = '\';

//input your other stuff

cout << m << slash << d << slash << y; //???

//what is activity?


Was This Post Helpful? 0
  • +
  • -

#5 Saige89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-February 09

Re: Writing dates with strings

Posted 20 February 2009 - 12:16 PM

activity is a string because i need to input like this

month/day/year activity for example

1/1/2009 New Year's
but the getline doesn't work
Was This Post Helpful? 0
  • +
  • -

#6 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Writing dates with strings

Posted 20 February 2009 - 12:18 PM

cin.getline(char *, int, char *);

In example:
#include <iostream>
using namespace std;

int main() {

     char Word[11] = {'\0'}; /* I always initiate to nulls */

     printf("Enter up to 10 letters: ");
     cin.getline(Word, 10, '\n');

     printf("You typed: %s", Word);
     cin.get();

     return 0;
}


Welcome, hope that helps.

This post has been edited by Hyper: 20 February 2009 - 12:20 PM

Was This Post Helpful? 0
  • +
  • -

#7 Saige89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-February 09

Re: Writing dates with strings

Posted 20 February 2009 - 12:41 PM

when i do cin.getline i get an error
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: Writing dates with strings

Posted 20 February 2009 - 01:11 PM

When you get an error, you need to tell us what the error is, so that we can help you. We can't see your screen, after all.
Was This Post Helpful? 0
  • +
  • -

#9 Saige89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-February 09

Re: Writing dates with strings

Posted 20 February 2009 - 02:03 PM

the code now is
 
#include <iostream>
#include <string>
using namespace std;

int main()
{
	int m, d, y;

	cout << "Enter calendar entries" << endl;
	cin >> m; 
	char slash = "/"
	cin >> slash;
	cin >> d;
	cin >> slash;
	cin >> y;
	string activity;
	getline (cin, activity);
	
	return 0;
	
}


the errors i get
invalid conversion from 'const char' to 'char'
invalid expected ',' or ';' before "cin"
Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: Writing dates with strings

Posted 20 February 2009 - 02:23 PM

Change this
char slash = "/"


to this
char slash = '/';


Double quotes are for strings, single quotes for characters (and you forgot the semi-colon).

This post has been edited by JackOfAllTrades: 20 February 2009 - 02:23 PM

Was This Post Helpful? 0
  • +
  • -

#11 Saige89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-February 09

Re: Writing dates with strings

Posted 20 February 2009 - 02:28 PM

ok it compiles fine but how do i get it to print like this:
month/day/year activity from the getline command
Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: Writing dates with strings

Posted 20 February 2009 - 02:40 PM

You don't need to read the slash (cin >> slash;), as you're setting it yourself with char slash = '/';.

So you've read the month into variable m, the day into variable d, the year into variable y, and the activity into the variable activity. Now you just print it out:

cout << m << slash << d << slash << y << " : " << activity << endl;

Was This Post Helpful? 0
  • +
  • -

#13 Saige89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-February 09

Re: Writing dates with strings

Posted 20 February 2009 - 02:56 PM

but i don't want to use cout because i want the program to print strings and spaces like this (_ = space)
month/day/year_activity

i know that the above worked
Was This Post Helpful? 0
  • +
  • -

#14 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Writing dates with strings

Posted 20 February 2009 - 03:00 PM

OK, let's make all that input one giant string:

char slash = '\';
cin.sync();
cin >> m;
cin.sync();
cin>> d;
cin.sync();
cin >> y;
cin.sync();
getline(cin, activity);

string wholeThing = m + slash + d  + slash + y + " " + activity;



No idea if it would compile as is, I just wrote it in the fast reply box...
Was This Post Helpful? 0
  • +
  • -

#15 Saige89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-February 09

Re: Writing dates with strings

Posted 20 February 2009 - 03:13 PM

ok i figure something out and it's working now thanks everyone
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1