newbie needs help with user input into strings

R

rob

hello all. im a newbie in C++ and programing in general. what i am
having problem with is taking user input, putting it into a string and
then displaying that string again. i am using the dev C++ compiler.
here is the code

#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{

string name;

cout << "Enter name: ";
cin >> name;
cout << endl << "Your name is " << name << endl;

cin.get();

return 0;
}

it will let me input what ever i want but nothing happens when i hit
enter, the window just stays blank until i hit enter again then the
window closes.

thanks for any help
 
A

Alf P. Steinbach

* rob:
hello all. im a newbie in C++ and programing in general. what i am
having problem with is taking user input, putting it into a string and
then displaying that string again. i am using the dev C++ compiler.

You're using the Dev C++ integrated development environment (IDE).

Probably that means you're using the g++ compiler.

But other compilers can be used with the same IDE.

here is the code

#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{

string name;

cout << "Enter name: ";
cin >> name;
cout << endl << "Your name is " << name << endl;

cin.get();

return 0;
}

it will let me input what ever i want but nothing happens when i hit
enter, the window just stays blank until i hit enter again then the
window closes.

Try running your program from a command interpreter.

For more help with that consult a group dedicated to your operating system.

Cheers, & hth.,

- Alf
 
D

djlad30

hello all. im a newbie in C++ and programing in general. what i am
having problem with is taking user input, putting it into a string and
then displaying that string again. i am using the dev C++ compiler.
here is the code

#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{

string name;

cout << "Enter name: ";
cin >> name;
cout << endl << "Your name is " << name << endl;

cin.get();

return 0;

}

it will let me input what ever i want but nothing happens when i hit
enter, the window just stays blank until i hit enter again then the
window closes.

thanks for any help


// there is no need to be including arguments u don't need.
// i just compiled this in .NET and it works fine
//when u running the code make sure u not in debug mode so it dosen't
blow by.




#include <iostream>
#include <string>


using namespace std;

int main()
{
string name;

cout <<"Enter your name _\b";

cin >> name;

cout <<endl;

cout << name<< endl;

return 0;


}
 
U

utab

hello all. im a newbie in C++ and programing in general. what i am
having problem with is taking user input, putting it into a string and
then displaying that string again. i am using the dev C++ compiler.
here is the code

#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{

string name;

cout << "Enter name: ";
cin >> name;
cout << endl << "Your name is " << name << endl;

cin.get();

return 0;

}

it will let me input what ever i want but nothing happens when i hit
enter, the window just stays blank until i hit enter again then the
window closes.

thanks for any help

Something related to implementation, There was sth related to conio
header and pause() function, try them and see what happens, your code
looks fine at first sight.

cin.get() just extracts the newline in the stream, and you are not
using that for anything why did you put that there?

HTH, Regards
 
O

osmium

rob said:
hello all. im a newbie in C++ and programing in general. what i am
having problem with is taking user input, putting it into a string and
then displaying that string again. i am using the dev C++ compiler.
here is the code

#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{

string name;

cout << "Enter name: ";
cin >> name;
cout << endl << "Your name is " << name << endl;

cin.get();

return 0;
}

it will let me input what ever i want but nothing happens when i hit
enter, the window just stays blank until i hit enter again then the
window closes.

Sometimes one cin.get() is not enough in DevC, your program needs two of
them To avoid such problems, I use this macro

#define STOP while(1) std::cin.get();

and then call it to freeze the display. It also looks neater, there are
other occasions where you will have to use the STOP macro as well.
 
D

David Harmon

On Wed, 17 Oct 2007 22:41:31 -0000 in comp.lang.c++, rob
having problem with is taking user input, putting it into a string

Look to the std::getline() function before anything else for that.
 
R

rob

forgive my lack of knowledge but i didn't understand most of what
anyone said.

1) im not sure how to run it in a command interpreter but i will look
into it.

2) i used #include <stdlib.h> because it was in the book, the book
hasn't really explained what any of this does in detail yet. Im ---
pretty sure im not in debug mode, i just use compile and run to
execute the program.

3) i used cin.get() at the end, so the program won't just quit when
its done and waits so i can actually see the output. is that ok? or is
---there a better way of doing this?

4) im not sure what you mean by that. but i took cin.get() out all
together and the program still acts the same way.

5) I have read up a bit about it on the internet but none of what i
read has really helped. could someone please point me in the right ---
direction?
 
O

osmium

rob said:
forgive my lack of knowledge but i didn't understand most of what
anyone said.

Did you try what I proposed? It *did* work for me on DevC. If the word
"macro" has no meaning to you, here is a chance to try to figure out what it
means. Wikipedia is often helpful in explaining geek-speak.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top