How can I print the integer taken as input without spaces? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I want to print out the number entered by the user as a whole and want to ignore the spaces.
Like this:
int aValue;
cin >> aValue;
Here, suppose the user enters 49 506, I want to print it as 49506.

First you need to get a string from a user with spaces, note you need to use std::getline() for that, as operator>> would not accept spaces:
std::string str;
std::getline( cin, str );
then you use std::remove_if() together with std::isspace() to remove spaces from your string:
auto it = std::remove_if( str.begin(), str.end(), []( unsigned char c ) { return std::isspace(c); } );
str.erase( it, str.end() );
and then you convert your string to an int using std::stoi():
auto aValue = std::stoi( str );
you should also add error checking to your code handling error conditions as described in documentation.

Related

how to parse line into number and string? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 11 months ago.
Improve this question
I would like to parse a sentence beginning with a number:
2 random sentece.
5 another one.
8 this is really long sentence.
Into int number holding the beginning number and the rest in std::string sentence. So in the first line, the parsing output will be number == 2 and sentence == "random sentence". The input is read from stdin, but the classical std::cin >> number >> sentence does'n work, since the parsing of string would end once it reaches a space. But I want to make the string beginning after the initial number to the end of line \n. So, how to do it in C++?
You can make use of std::getline and std::istringstream as shown below. In particular in the given program, std::getline is used to read line by line and std::istringstream is used to read first the integer and then the remaining sentence.
#include <iostream>
#include <string>
#include <sstream>
int main()
{
std::string sentence;
int inputInt = 0;
//read line by line
while(std::cin >> inputInt && std::getline(std::cin, sentence))
{
std::cout<< inputInt <<"-----"<<sentence<<std::endl;
//do the check here
}
}
Demo

How can I add letters in a sentence? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I've asked to write code that gets a char array(sentence), if the there is an 'i' in the sentence I need to add the letter 'b' the letter 'i' again like this example:
pig -> pibig
I tried to use string.h functions but I didn't succeed to make it right.
Use std::string in string header file, and std::string::insert whenever you need to insert a char in string:
std::string my_string = "my satringa";
for (size_t i = 0; i < my_string.length(); ++i)
{
if (my_string.at(i) == 'a')
{
my_string.insert(i + 1, "b");
}
}
std::clog << my_string << std::endl;
Output:
> my sabtringab
If you are forced to use C-style strings, don't worry do all of your operations on std::string and then take the underlying stored string with std::string::c_str() as a C-style string (and don't forget to take a copy).

Just input a number in cpp [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm need to create id just number. For example: 12345678
If user inputs fail( contain char), delete char immediately.
For example: 123a =>input gain!
Please help me!
I think this is what you are looking for:
#include <iostream>
#include <string>
#include <cctype>
int main () {
std::string input;
bool valid;
do {
valid = true;
std::cin >> input;
for (char c : input)
if (! std::isdigit( static_cast<unsigned char>(c) ) )
valid = false;
} while (! valid);
// Here the string is guaranteed to be valid
}
Be aware though, that whatever you are trying to do, this does not look like the proper way to do it. There are ways to read numbers in c++, and this is not what I would recommend.

C++ reading text from file and storing it in separate variables [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
My .txt file is like this:
question1 answer1
question2 answer2
question3 answer3
How can I place question1 and answer1 into two separate variables? I can use getLine(), but it will return the question and answer.
If each question is terminated with a question mark then you can write
std::string line;
while ( std::getline( FileStream, line ) )
{
std::istringstream is( line );
std::string question;
std::string answer;
std::getline( is, question, '?' );
question += '?';
std::getline( is, answer );
// some processing of question and answer
}
If there is used some other separator then you need to substitute the question mark for this separator and maybe to remove line
question += '?';

What is the most efficient C++ method to split a string based on a particular delimiter similar to split method in python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
The community reviewed whether to reopen this question 2 months ago and left it closed:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
Improve this question
getline(cin,s);
istringstream iss(s);
do
{
string sub;
iss>>sub;
q.insert(sub);
}while(iss);
I used this technique when question wanted me to split on the basis of space so can anyone explain me how to split when there's a particular delimiter like ';' or ':'.
Someone told me about strtok function but i am not able to get it's usage so it would be nice if someone could help.
First, don't use strtok. Ever.
There's not really a function for this in the standard library.
I use something like:
std::vector<std::string>
split( std::string const& original, char separator )
{
std::vector<std::string> results;
std::string::const_iterator start = original.begin();
std::string::const_iterator end = original.end();
std::string::const_iterator next = std::find( start, end, separator );
while ( next != end ) {
results.push_back( std::string( start, next ) );
start = next + 1;
next = std::find( start, end, separator );
}
results.push_back( std::string( start, next ) );
return results;
}
I believe Boost has a number of such functions. (I implemented
most of mine long before there was Boost.)