#input int next to input string
1 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.
need more info
Anyone can ask a question in our programming channels. Following the guide Writing The Perfect Question is recommended.
State your problem clearly and provide all necessary details:
• the relevant portion of your code, or all of it
• the expected output
• the actual output (or the full error)
🏆 Gold Standard: Minimal Reproducible Example
Provide the relevant code in the message, and format it nicely with a code block*. If it's too much for one message, you can upload it:
• Compiler Explorer for most C/C++ snippets
• OnlineGDB for interaction, debugging
⛔ Do not post screenshots, let alone photos of your screen!
#include <iostream>
using namespace std;
int main()
{
//set vars
string colour, plNoun, noun, celeb, food;
int num1;
//get user input
cout << "Enter a colour: ";
getline(cin, colour);
cout << "Enter a plural noun: ";
getline(cin, plNoun);
cout << "Enter a number: ";
cin >> num1;
cout << "Enter a noun: ";
getline(cin, noun);
cout << "Enter a celebrity: ";
getline(cin, celeb);
cout << "Enter a food: ";
getline(cin, food);
//output thingy
cout << "I woke up on my " << noun << " today." << endl;
cout << "I had " << plNoun << " all over my floor." << endl;
cout << "Luckily, " << celeb << " gave me a " << colour << " " << food << " so it was okay!";
cout << num1;
return 0;
}
This is the code i used and when i added the screenshot of the console happenedcout << "Enter a number: "; cin >> num1;
sorry if that isnt enough information but i dont really know what the problem is
it looks reasonable at first, I think the problem may be related to whether newlines are left remaining in the stream or not
I'm not really sure, I don't generally use this kind of approach for input
try making everything use getline and then use std::from_chars to convert from string to number
how would i use std::from_chars in the code? sorry if this is an incompetent question but i started c++ just over 2 hours ago
ok
https://en.cppreference.com/w/cpp/utility/from_chars has examples
;compile
#include <iostream>
#include <string_view>
#include <charconv>
using namespace std;
int main()
{
constexpr string_view numStr = "1234";
long num1 = 0;
from_chars(numStr.data(), numStr.data() + numStr.size(), num1);
cout << "num1: " << num1 << endl;
}
num1: 1234
but there's a simplified example for you
Thank you and let us know if you have any more questions!
[SOLVED] input int next to input string
@twin pike
This question thread is being automatically marked as solved.