Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > newbie needs help with user input into strings

Reply
Thread Tools

newbie needs help with user input into strings

 
 
rob
Guest
Posts: n/a
 
      10-17-2007
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

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-17-2007
* 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

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
djlad30@hotmail.com
Guest
Posts: n/a
 
      10-18-2007
On Oct 17, 5:41 pm, rob <(E-Mail Removed)> wrote:
> 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;


}

 
Reply With Quote
 
utab
Guest
Posts: n/a
 
      10-18-2007
On Oct 18, 12:41 am, rob <(E-Mail Removed)> wrote:
> 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


 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      10-18-2007
"rob" writes:

> 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.


 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      10-18-2007
On Wed, 17 Oct 2007 22:41:31 -0000 in comp.lang.c++, rob
<(E-Mail Removed)> wrote,
>having problem with is taking user input, putting it into a string


Look to the std::getline() function before anything else for that.
 
Reply With Quote
 
rob
Guest
Posts: n/a
 
      10-19-2007
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?

 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      10-20-2007
"rob" wrote:

> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie -needs help displaying into textarea from a db Joe Todd HTML 2 11-12-2008 09:31 PM
Newbie needs help getting user input Peter Vanderhaden Ruby 12 09-30-2007 04:26 PM
Entering strings as user input but interpreting as Python input (sortof) Chris Carlen Python 1 09-18-2007 01:58 AM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
Newbie needs help with regex strings Catalina Scott A Contr AFCA/EVEO Python 5 12-14-2005 07:31 PM



Advertisments