Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 how to get a string
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
endusto




PostPosted: Tue May 10, 2005 6:43 pm   Post subject: how to get a string

im just starting and i have a program that will ask you for a number and then tell you the number you entered. how would i make a string so i can enter a word or two instead of just numbers or 1 letter?


this is my code:
code:

#include <iostream>

using namespace std;

int main() {
    int cat;
cout<<"hey";
cin>>cat;
cin.ignore();
cout<<cat;
cin.get();
}
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Tue May 10, 2005 6:54 pm   Post subject: (No subject)

Declare a string.

code:
string foo;


Then exatract a string from cin into it.

code:
cin >> foo;


You now have a "word" (delimited by a space).
blackcat




PostPosted: Wed May 18, 2005 12:46 pm   Post subject: (No subject)

int is for numbers
so declare a string liek wtd said


so lets use

code:
#include <iostream>
using namespace std;
int main ()
{
  string foo;  //* this is the variable fag
  cout << "Type a bad word you hate people using\n\n";  //* ask the question
  cin >> foo; //* get answer which is a string because string foo;
  cout << endl << "\nyou are a " << foo << "you got fooled!";
  return 0;
}

EDIT[MARTIN]:Grow up. Also, use code tags in the future.
jamonathin




PostPosted: Wed May 18, 2005 1:38 pm   Post subject: (No subject)

Lets also use
code:
[code][/code]

And we need this at the top as well.
c++:

#include <string>

<<or so I think>>

And you're going to want to put this before your return 0; So that you can read what the program does.
c++:

int wait;
cin >> wait;
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: