Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 11:56

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Convert string to integer, Frage...

Discussion on Convert string to integer, Frage... within the C/C++ forum part of the Coders Den category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Aug 2012
Posts: 3
Received Thanks: 0
Question Convert string to integer, Frage...

Hallo,
also, es geht um zwei Codes. Beide sollen einen String in eine Zahl umwandeln.
Um zu zeigen worum es genau geht, hier der funktionierende Code:
PHP Code:
#include <iostream>
#include <string>
#include <sstream>

int main ()
{
    
std::string age;
    
int xh;

    
std::cout << "Enter age for x: ";
    
std::getline (std::cinage);
    
std::stringstream (age) >> x;

    
std::cout << "\nEnter age for y: ";
    
std::getline (std::cinage);
    
std::stringstream (age) >> h;

    
std::cout << "\n\nFinal result: " << h;

    
system ("pause>nul");

    return 
0;

Beispielausgabe bei Ausführung:

Enter age for x: (Eingabe: 7)

Enter age for y: (Eingabe: 8)

Final result: 56



Ich habe versucht das selbe zu erreichen, nur mit anderen Code:
PHP Code:
#include <iostream>
#include <string>

int main ()
{
    
std::string age;
    
int xh;

    
std::cout << "Enter age for x: ";
    
std::getline (std::cinage) >> x;

    
std::cout << "\nEnter age for y: ";
    
std::getline (std::cinage) >> h;

    
std::cout << "\n\nFinal result: " << h;

    
system ("pause>nul");

    return 
0;

Hier die ungewünschte Ausgabe:
Enter age for x: (Eingabe: 7)
//Dann passiert nichts, es wird lediglich auf die nächste Zeile übergesprungen.
//Ich gebe z.B 4 ein und erst dann kommt:
Enter age for y: (Eingabe: 9)
Final result: 36
// Und das ist das Problem! Ich muss oben etwas dazu eingeben und dann wird auch noch was ganz anderes multipliziert anstatt was ich möchte.
int_Max is offline  
Old 08/10/2012, 00:42   #2


 
MrSm!th's Avatar
 
elite*gold: 7110
Join Date: Jun 2009
Posts: 28,902
Received Thanks: 25,407
Warum nutzt du nicht einfach std::cin an Stelle von getline?
std::cin hat den Operator >> überladen und führt automatisch eine formatierte Eingabe durch:

Code:
#include <iostream>
#include <string>
#include <sstream>

int main ()
{
    int x, h;

    std::cout << "Enter age for x: ";
    std::cin >> x;

    std::cout << "\nEnter age for y: ";
    std::cin >> h;

    std::cout << "\n\nFinal result: " << x * h;

    system ("pause>nul");

    return 0;
}
MrSm!th is offline  
Old 08/10/2012, 01:32   #3
 
elite*gold: 0
Join Date: Jul 2010
Posts: 388
Received Thanks: 196
Code:
#include <iostream>
#include <string>

using namespace std;

int main ()
{
    string age;

    cout<<cin<<endl;

    cout<<getline (cin, age)<<endl;

    return 0;
}
Lass' das mal laufen. Die Ausgaben stimmen überein. Du rufst getline mit cin auf, die Konsole nimmt eine Eingabe, getline gibt allerdings den ersten Parameter als Rückgabewert zurück, in dem Fall cin. Der getline Aufruf wird also zu cin ausgewertet, und dann rufst du operator>> auf das zurückgelieferte cin von getline auf. Deshalb macht die Konsole auch nichts nach der ersten Eingabe, sie wartet auf noch eine.
SmackJew is offline  
Thanks
1 User
Old 08/10/2012, 07:50   #4
 
elite*gold: 5
Join Date: Sep 2006
Posts: 385
Received Thanks: 218
Quote:
Originally Posted by MrSm!th View Post
Warum nutzt du nicht einfach std::cin an Stelle von getline?
std::cin hat den Operator >> überladen und führt automatisch eine formatierte Eingabe durch:

Code:
#include <iostream>
#include <string>
#include <sstream>

int main ()
{
    int x, h;

    std::cout << "Enter age for x: ";
    std::cin >> x;

    std::cout << "\nEnter age for y: ";
    std::cin >> h;

    std::cout << "\n\nFinal result: " << x * h;

    system ("pause>nul");

    return 0;
}
Weil std::cin:: operator>> und std::getline anders funktionieren. std::getline ließt und leert den Stream bis zum nächsten newline (oder einem eigenen Delimeter), std::cin:: operator>> nur bis das Format nicht mehr stimmt.

Ein paar Testwerte in deinem Beispiel:
Enter age for x: 123junk

Enter age for y: <wird übersprungen>

Final result: <whatever>

Nun das selbe mit dem getline Beispiel des TEs:
Enter age for x: 123junk

Enter age for y: 2more junk

Final result: 246


@TE:
Wie SmackJew bereits sagte machst du quasi das hier im zweiten Beispiel:
Code:
std::getline(...);
std::cin >> x;
std::getline(...);
std::cin >> y;
Nightblizard is offline  
Thanks
1 User
Reply


Similar Threads Similar Threads
Convert to string ?
05/23/2012 - AutoIt - 1 Replies
Hello guys. I have an int and I want to convert it into a string. Not just normal conversion of the type, but a string from an int16 In vb.net it is done like this : Convert.ToString(integer, 16) Say the integer is 999 I do not want it to be converted into 999 string but the output should be this ---> "3e7" How can this be done in AutoIT ?
[Visual Basic] [Problem] String auslesen/String zufällig wählen
05/06/2012 - General Coding - 4 Replies
Code: #include <GUIConstants.au3> #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Name Generator", 236, 299, 815, 246) $Input1 = GUICtrlCreateInput("Username", 24, 72, 185, 21) $Input2 = GUICtrlCreateInput("Username", 24, 104, 185, 21) $Input3 = GUICtrlCreateInput("Username", 24, 136, 185, 21) $Input4 = GUICtrlCreateInput("Username", 24, 168, 185, 21) $Input5 = GUICtrlCreateInput("Username", 24, 200, 185, 21)
c++ WinApi integer ausgeben
11/26/2011 - C/C++ - 8 Replies
Hay, also ich bin nun neu in der WinApi mit c++ und versuche mich grad an einem einfachen Rechner(ohne WinApi natürlich kein Problem). Wie im Titel steht schaffe ich es nicht per "TextOut" oder per "MessageBox" einen integer ausgeben zu lassen. Ich hab schon gegooglet etc. bloß bin ich daraus nicht sehr viel schlauer geworden. Bis jetzt hab ich halt das(Ausschnitt): ...
small help how to convert string to hex to make a new StatusEffect korvacs arco pro4e
07/16/2010 - CO2 Private Server - 4 Replies
now i want to know how to convert string to hexdemical to make a new StatusEffectEn in 5165 scours or give me hexdemical for this mate_pk_02 mate_pk_03 mate_pk_08 moonpk_eight moonpk_second moonpk_third ninjapk_eight
[Help]C# How to convert memory value to string?
05/04/2010 - CO2 Programming - 2 Replies
How to convert value to string :confused: this is my code Is this code right or is it wrong? thanks in advance!:handsdown: note: I know that CloseHandle is missing :) using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq;



All times are GMT +1. The time now is 11:57.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.