std::cout << "Enter age for x: "; std::getline (std::cin, age); std::stringstream (age) >> x;
std::cout << "\nEnter age for y: "; std::getline (std::cin, age); std::stringstream (age) >> h;
std::cout << "\n\nFinal result: " << x * 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 x, h;
std::cout << "Enter age for x: "; std::getline (std::cin, age) >> x;
std::cout << "\nEnter age for y: "; std::getline (std::cin, age) >> h;
std::cout << "\n\nFinal result: " << x * 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.
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;
}
#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.
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:
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 ?
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/05/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;