#input int next to input string

1 messages · Page 1 of 1 (latest)

twin pike
#

I am using in this piece of code but it doesnt let me input something after the input number have i missed out something?

vital crystalBOT
#

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.

ruby umbra
#

need more info

open crownBOT
#
How to Ask A Programming Question

Anyone can ask a question in our programming channels. Following the guide Writing The Perfect Question is recommended.

What To Post

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

Where To Post

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!

twin pike
#
#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

ruby umbra
#

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

twin pike
#

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

ruby umbra
#

;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;
}
crystal quiverBOT
#
Program Output
num1: 1234
ruby umbra
#

but there's a simplified example for you

twin pike
#

thank you

#

!solved

vital crystalBOT
#

Thank you and let us know if you have any more questions!

#

[SOLVED] input int next to input string

vital crystalBOT
#

@twin pike

This question thread is being automatically marked as solved.