PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > newbie needs with user input into strings
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
newbie needs with user input into strings

Réponse
 
LinkBack Outils de la discussion
Vieux 17/10/2007, 23h41   #1
rob
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut newbie needs with user input into strings

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

  Réponse avec citation
Vieux 18/10/2007, 00h57   #2
Alf P. Steinbach
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: newbie needs with user input into strings

* 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 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?
  Réponse avec citation
Vieux 18/10/2007, 01h04   #3
djlad30@hotmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: newbie needs with user input into strings

On Oct 17, 5:41 pm, rob <rob1...@gmail.com> 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



// 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;


}

  Réponse avec citation
Vieux 18/10/2007, 01h46   #4
utab
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: newbie needs with user input into strings

On Oct 18, 12:41 am, rob <rob1...@gmail.com> 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


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


  Réponse avec citation
Vieux 18/10/2007, 02h36   #5
osmium
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: newbie needs with user input into strings

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


  Réponse avec citation
Vieux 18/10/2007, 08h02   #6
David Harmon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: newbie needs with user input into strings

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


Look to the std::getline() function before anything else for that.
  Réponse avec citation
Vieux 19/10/2007, 21h45   #7
rob
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: newbie needs with user input into strings

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 ed. could someone please point me in the right ---
direction?

  Réponse avec citation
Vieux 20/10/2007, 01h54   #8
osmium
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: newbie needs with user input into strings

"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 ful in explaining geek-speak.


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 20h31.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14042 seconds with 16 queries