简体   繁体   中英

C++ reading in both an int and a string, possibly at the same time

Fairly new to C++ here, but not new to programming. I was wondering if there is any simple way to get a user input such as "20 kg", the '20' being whatever the user input, and then the kg/lb/etc being, again, what the user input. The thing is, i need to use the integer part of the input in a calculation. What i am thinking of having to do is read it all in as a String and then separate the int and string into separate variables. (i will have to use both the number and the measurement type in an equation) any help would be great.

I'm not looking for any block of code, i merely want both an explanation of what i should do, and any key code snippets i may need to use. Thank you in advance!

std::istream (specifically the operator >>() s) can handle this situation easily enough:

int weight;
std::string units;
std::cout << "Guess the weight of the cake: ";
if (std::cin >> weight >> units)
{
    std::cout << weight << units << "? Spot on!" << std::endl;
}
else
{
    std::cerr << "Expected a numeric weight and alphabetic units (e.g: 42 kg)."
              << std::endl;
}

Use pair<int, string> by considering them as a whole, easy to handle afterwards.

pair<int, string> val;
if (cin >> val.first >> val.second) 
    // read input sucessfully, e.g. val will be {20, "kg"}
else 
    cerr << "unable to input weight and units\n"

After this, whenever you want to calculate, just use val.first . And use val.second for the measurement.

PS : you can use pair<float, string> if you need to handle float numbers.

First you have to make sure the input has a space between the integer part and the metric part. Then You should

  1. split it into two parts and

  2. convert the first part into an integer.

If you don't want to do such tedious work yourself, you can use ssstream . Below is a short sample.

#include<string.h>
#include<iostream>
#include<sstream>

using namespace std;

int main()
{
    string input("20 kg");

    istringstream stream(input);

    int n;
    string metric;

    stream >> n;
    stream >> metric;

    //do something you want here

    cout<<n<<" "<<metric;

    return 0;
}

My idea would be to have the user enter the entire thing as a string and then you can just use the substr method to split the string up into the number portion and then the measurement portion. Then you would have to convert the number portion into an integer.

example

string str = "20 lb";
string delimiter = " ";  //space
string number = str.substr(0, str.find(delimiter)); // this will get you the number
string measurement = str.substring(str.find(delimiter)+1, str.length()) //this will get you the               measurement
//convert the number string now

that should work for you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM