C++ - Storing user input string separated by commas into vector - c++

I have a code written that performs this task to a certain extent. But, I would like to how to alter my code so that I can store as many string inputs the user wants to enters into the vector.
Here is my code:
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
int main ()
{
string input = "";
cout << "Input: ";
cin >> input;
string a,b;
for(int i = 0; i<input.size(); i++)
{
if(input.at(i)==','){
a=input.substr(0,i);
b=input.substr(i+1);
}
}
vector<string> objects;
objects.push_back(a);
objects.push_back(b);
for (int k = 0; k < 2; k++) {
cout << objects[k] << endl;
}
return 0;
}
So far, it can only recognize and store two inputs separated by commas. I am very new to coding so could someone show me a way to make this into a loop and take in as many inputs as the user enters?
Thank you.

There are much simpler approaches to parse an input string using stringstreams:
string a;
vector<string> objects;
for(stringstream sst(input); getline(sst, a, ','); ) // that's all !
objects.push_back(a);
copy (objects.begin(), objects.end(), ostream_iterator<string>(cout," ; ")); // display all
Online demo

You need to change your code in order to work for any number of user input.
The logic is to push every sub string between the commas into vector.
vector<string> objects;
for(int i = 0,j=0; i<input.size(); i++)
{
if(input.at(i)==',' || input.at(i)=='\0'){
objects.push_back(input.substr(j,i-j)); //pushing the sub string
j=i+1;
}
}
In order to print the vector first you have to find the size of the vector,then simply iterate over to print it.
//display
int l=objects.size();
for (int k = 0; k < l; k++) {
cout << objects[k] << endl;
}
Note: If you want your code to work for strings with spaces in between , for example: a ,b ,c ,d then use getline(cin,input); to take input from user.

You can see running code here or as a github gist.
// Example program
#include <iostream>
#include <string>
#include <vector>
#include <string>
void ParseCSV(
std::vector< std::string >& output,
const std::string& csv )
{
int q = 0;
int p = csv.find(",");
while( p != -1 )
{
output.push_back( csv.substr(q,p-q) );
q = p+2;
p = csv.find(",",q);
}
// The terminating comma of the CSV is missing
// so we need to check if there is
// one more value to be appended
p = csv.find_last_of(",");
if( p != -1 )
{
output.push_back( csv.substr( p+2 ) );
}
else
{
// there was no comma
// this could be because the list is empty
// it could also be because there is just one element in the list
if( csv.length() > 1 )
output.push_back( csv );
}
}
int main()
{
std::string test("this is my list, a, b, c, d, end of line");
std::vector< std::string > split;
ParseCSV( split, test );
for( auto& s : split )
std::cout << s << std::endl;
}
As suggested by Christophe, using stringstream is much better. No special case handling needed! I use a while loop - it seems clearer what is happening.
void ParseCSV2(
std::vector< std::string >& output,
const std::string& csv )
{
std::stringstream sst(csv);
std::string a;
while( getline( sst, a, ',' ) )
output.push_back(a);
}

Related

C++ reverse a string but printing numbers first

I was given a project in class and almost have it finished, I am required to take a string of numbers and letters and return that string with the numbers printed first followed by the letters in reverse order (ex. abc123 should return 123cba). As of now my code returns a string with the numbers first and the original order of the letters (ex. abc123 returns 123abc). I would be able to do this with two loops however the assignment asks that my code only iterates though the initial string one time. Here is the code I have so far...
#include <iostream>
#include <string>
#include "QueType.h"
#include "StackType.h"
using namespace std;
int main ()
{
QueType<char> myQueue;
StackType<char> myStack;
string myString="hello there123";
char curchar;
string numbers, letters;
for (int i = 0; i < myString.length(); i++) {
if (isdigit(myString.at(i))) {
myQueue.Enqueue(myString.at(i));
myQueue.Dequeue(curchar);
numbers += curchar;
//cout<<numbers<<endl;
}
else if (islower(myString.at(i))) {
myStack.Push(myString.at(i));
curchar = myStack.Peek();
myStack.Pop();
letters += curchar;
//cout<<curchar<<endl;
}
}
cout<<(myString = numbers + letters)<<endl;
}
In my code, I have two .h files that set up a stack and a queue. With the given string, the code loops through the string looking to see if it sees a letter or number. With a number the spot in the string is then saved to a queue, and with a letter it is saved to the stack.
The only other way i can think of reversing the order of the letters is in the if else statement instead of having char = myStack.Peek() every loop, change it to char += myStack.Peek() however I get weird lettering when that happens.
since you already got the string with letters you can basically reverse it and that's it.
//emplace version:
void reverse_str(std::string& in)
{
std::reverse(in.begin(), in.end());
}
//copy version
std::string reverse_str(std::string in)
{
std::reverse(in.begin(), in.end());
return in;
}
in your case the emplace version would be the best match.
in other cases (e.g. when you want to preserve the original string) the copy version is preferred.
adding an example to make it as clean as possible.
int main()
{
std::string inputstr = "123abc";
std::string numbers{};
std::string letters{};
for(auto c : inputstr)
{
if(isdigit(c))
numbers += c;
else
letters += c;
}
reverse_str(letters); //using the emplace version
std::cout << numbers + letters;
}
Here's my take. It only loops through the string once. I don't have your types, so I'm just using the std versions.
std::string output;
output.reserve( myString.size() );
std::stack<char> stack;
for ( char c : myString ) {
if ( std::isdigit( c ) ) // if it's a number, just add it to the output
output.push_back( c );
else // otherwise, add the character to the stack
stack.push( c );
}
// string is done being processed, so use the stack to get the
// other characters in reverse order
while ( !stack.empty() ) {
output.push_back( stack.top() );
stack.pop();
}
std::cout << output;
working example: https://godbolt.org/z/eMazcGsMf
Note: wasn't sure from your description how to handle characters other than letters and numbers, so treated them the same as letters.
One way to do this is as follows:
Version 1
#include <iostream>
#include <string>
int main() {
std::string s = "abc123";
std::string output;
output.resize(s.size());
int i = output.length() - 1;
int j = 0;
for(char &c: s)
{
if(!std::isdigit(c))
{
output.at(i) = c;
--i;
}
else
{
output.at(j) = c;
++j;
}
}
std::cout<<output<<std::endl;
}
You can also use iterators in the above program to obtain the desired result as shown in version 2.
Version 2
#include <iostream>
#include <string>
int main() {
std::string s = "abfsc13423";
std::string output;
output.resize(s.size());
std::string::reverse_iterator iter = output.rbegin();
std::string::iterator begin = output.begin();
for(char &c: s)
{
if(!std::isdigit(c))
{
*iter = c;
++iter;
}
else
{
*begin = c;
++begin;
}
}
std::cout<<output<<std::endl;
}

A quick question from a newbie about deleting non-Upper-alpha: why isn't my code working?

Hi I am trying to delete all the non-capitalized alphabet from a string input, but I am not quite sure where the error is in my coding. Please comment if you know why!
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
string CreateAcronym(string userPhrase) {
int i;
int stringSize;
char charAti;
stringSize = userPhrase.size();
for (i=0 ; i < stringSize ; i++ ) {
charAti = userPhrase.at(i);
if ( !isupper(charAti)) {
userPhrase.erase(i,1);
}
}
return userPhrase;
}
int main() {
string userSentence;
getline(cin , userSentence);
cout << CreateAcronym(userSentence) << endl;
return 0;
}
You cached old string length and continued to use while the string will become shorter by erasing characters.
You skip characters after characters to erase because i++ isn't canceled after erasure.
stringSize = userPhrase.size();
for (i=0 ; i < stringSize ; i++ ) {
charAti = userPhrase.at(i);
if ( !isupper(charAti)) {
userPhrase.erase(i,1);
}
}
should be
for (i=0 ; i < static_cast<int>(userPhrase.size()) ; ) {
charAti = userPhrase.at(i);
if ( isupper(charAti)) {
i++;
} else {
userPhrase.erase(i,1);
}
}
The problem have been answered by others, so I just add my "simpler" solution to the problem:
string CreateAcronym(string userPhrase) {
string result; // Create an empty string
// Loop over all the characters in the original string
for (char c : userPhrase) {
// If the character is upper-case...
if (isupper(c))
result += c; // Append it to the new string
}
return result; // Return the new string
}
You have 2 issues in your code.
First, you are erasing the string inside the loop (which changes its length), but using the precalculated length in the comparison.
Second, you only need to increment i when you don't erase a character. Otherwise, you will skip over some characters.
A working loop would be:
for (i = 0; i < userPhrase.size();) {
charAti = userPhrase.at(i);
if ( !isupper(charAti)) {
userPhrase.erase(i,1);
}
else {
++i;
}
}
You could simplify this loop by using an algoritm:
string CreateAcronym(string userPhrase) {
userPhrase.erase(std::remove_if(userPhrase.begin(),
userPhrase.end(), [](auto charAti) {
return !isupper(charAti); }),
userPhrase.end());
return userPhrase;
}
Here's a demo.

C++ Problem with space detection in an Array String

I'm currently writting a program where I try to filter extra spaces so if there are more than 1 spaces in a row, I discard the rest leaving only one
But this is only the first step because the aim of the program is to parse a txt file with mips assembly instructions.
So far I've opened the file, stored the content in a vector and then stored the vector content in an array. Then I check, if you find a char 2 times in a row shift the array to the left.
The problem is that the code works well for any other letter, except for the space character. (On the code below I test it with the 'D' character and it works)
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;
class myFile {
vector<string> myVector;
public:
void FileOpening();
void file_filter();
};
void myFile::FileOpening() {
string getcontent;
ifstream openfile; //creating an object so we can open files
char filename[50];
int i = 0;
cout << "Enter the name of the file you wish to open: ";
cin.getline(filename, 50); //whatever name file the user enters, it's going to be stored in filename
openfile.open(filename); //opening the file with the object I created
if (!openfile.is_open()) //if the file is not opened, exit the program
{
cout << "File is not opened! Exiting the program.";
exit(EXIT_FAILURE);
};
while (!openfile.eof()) //as long as it's not the end of the file do..
{
getline(openfile, getcontent); //get the whole text line and store it in the getcontent variable
myVector.push_back(getcontent);
i++;
}
}
void myFile::file_filter() {
unsigned int i = 0, j = 0, flag = 0, NewLineSize, k, r;
string Arr[myVector.size()];
for (i = 0; i < myVector.size(); i++) {
Arr[i] = myVector[i];
}
//removing extra spaces,extra line change
for (i = 0; i < myVector.size(); i++) {
cout << "LINE SIZE" << myVector[i].size() << endl;
for (j = 0; j < myVector[i].size(); j++) {
//If I try with this character for example,
//it works (Meaning that it successfully discards extra 'Ds' leaving only one.
// But if I replace it with ' ', it won't work. It gets out of the loop as soon
//as it detects 2 consecutive spaces.
if ((Arr[i][j] == 'D') && (Arr[i][j + 1] == 'D')) {
for (k = j; k < myVector[i].size(); k++) {
Arr[i][k] = Arr[i][k + 1];
flag = 0;
j--;
}
}
}
}
for (i = 0; i < myVector.size(); i++) {
for (j = 0; j < myVector[i].size(); j++) //edw diapernw tin kathe entoli
{
cout << Arr[i][j];
}
}
}
int main() {
myFile myfile;
myfile.FileOpening();
myfile.file_filter();
}
My question is, why does it work with all the characters except the space one, and how do I fix this?
Thanks in advace.
Wow. Many lines of code. I can only recomend to learn more about the STL and algorithms.
You can read the complete file into a vector using the vectors "range"-constructor and std::istream_iterator. Then you can replace one or more spaces in a string by using a std::regex. This is really not complicated.
In the below example, I do all the work, with 2 lines of code in function main. Please have a look:
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <string>
#include <fstream>
#include <regex>
using LineBasedTextFile = std::vector<std::string>;
class CompleteLine { // Proxy for the input Iterator
public:
// Overload extractor. Read a complete line
friend std::istream& operator>>(std::istream& is, CompleteLine& cl) { std::getline(is, cl.completeLine); return is; }
// Cast the type 'CompleteLine' to std::string
operator std::string() const { return completeLine; }
protected:
// Temporary to hold the read string
std::string completeLine{};
};
int main()
{
// Open the input file
std::ifstream inputFile("r:\\input.txt");
if (inputFile)
{
// This vector will hold all lines of the file. Read the complete file into the vector through its range constructor
LineBasedTextFile text{ std::istream_iterator<CompleteLine>(inputFile), std::istream_iterator<CompleteLine>() };
// Replace all "more-than-one" spaces by one space
std::for_each(text.begin(), text.end(), [](std::string& s) { s = std::regex_replace(s, std::regex("[\\ ]+"), " "); });
// For Debug purposes. Print Result to std::out
std::copy(text.begin(), text.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
}
return 0;
}
I hope, I could give you some idea on how to proceed.

Reading numbers from file to string to array

I am reading numbers from a file, say:
1 2 3 4 5
I want to read this data from a file into a string into an array for further processing. Here's what I've done:
float *ar = nullptr;
while (getline(inFile, line))
{
ar = new float[line.length()];
for (unsigned int i = 0; i < line.length(); i++)
{
stringstream ss(line);
ss >> ar[i];
}
}
unsigned int arsize = sizeof(ar) / sizeof(ar[0]);
delete ar;
Suffice it to say that it works insofar it only gets the first value from the file. How do I get the array to be input ALL the values? I debugged the program and I can confirm that line has all the necessary values; but the float array doesn't. Please help, thanks!
line.length() is the number of characters in the line, not the number of words/numbers/whatevers.
Use a vector, which can be easily resized, rather than trying to juggle pointers.
std::vector<float> ar;
std::stringstream ss(line);
float value;
while (ss >> value) { // or (inFile >> value) if you don't care about lines
ar.push_back(value);
}
The size is now available as ar.size(); your use of sizeof wouldn't work since ar is a pointer, not an array.
The easiest option is to use the standard library and its streams.
$ cat test.data
1.2 2.4 3 4 5
Given the file you can use the stream library like this:
#include <fstream>
#include <vector>
#include <iostream>
int main(int argc, char *argv[]) {
std::ifstream file("./test.data", std::ios::in);
std::vector<float> res(std::istream_iterator<float>(file),
(std::istream_iterator<float>()));
// and print it to the standard out
std::copy(std::begin(res), std::end(res),
std::ostream_iterator<float>(std::cout, "\n"));
return 0;
}
I ran into this problem earlier when I wanted to extract data line by line from a file to fill my sql database that I wanted to use.
There are many solutions to this specific problem such as:
The solution is using stringstream with a while statement to put data from file into the array with a while statement
//EDIT
While statement with getline
//This solution isn't very complex and is pretty easy to use.
New Improved simple solution:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
ifstream line;
line.open("__FILENAME__");
string s;
vector<string> lines;
while(getline(line, s))
{
lines.push_back(s);
}
for(int i = 0;i < lines.size();i++)
{
cout << lines[i] << " ";
}
return 0;
}
compiled code to check - http://ideone.com/kBX45a
What about atof?
std::string value = "1.5";
auto converted = atof ( value.c_str() );
Rather complete:
while ( std::getline ( string ) )
{
std::vector < std::string > splitted;
boost::split ( splitted, line, boost::is_any_of ( " " ) );
std::vector < double > values;
for ( auto const& str: splitted ) {
auto value = atof ( str.c_str() );
values.push_back ( value );
}
}

I need help explaining the behaviour of this code

I've been having a lot of trouble with the concept of an array of struct. I put together some basic code. The output from this code was not what I expected at all. I was wondering if someone could explain why this code behaves the way it does.
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>
struct DataRow
{
std::string word1;
std::string word2;
std::string word3;
};
void getRow( std::string line, DataRow** dataRowPointer, int index )
{
std::stringstream sline(line);
std::string word;
int wordIndex = 0;
*dataRowPointer = new DataRow[3];
while( sline >> word )
{
if ( wordIndex == 0 )
{ (*dataRowPointer)[index].word1 = word; }
else if ( wordIndex == 1 )
{ (*dataRowPointer)[index].word2 = word; }
else
{ (*dataRowPointer)[index].word3 = word; }
wordIndex++;
}
}
int main( )
{
int index = 0;
std::string line1 = "This is line";
std::string line2 = "Two is magic";
std::string line3 = "Oh Boy, Hello";
DataRow* dataRowPointer;
getRow( line1, &dataRowPointer, index );
index++;
getRow( line2, &dataRowPointer, index );
index++;
getRow( line3, &dataRowPointer, index );
for( int i = 0; i < 3; i++ )
{
std::cout << dataRowPointer[i].word1 << dataRowPointer[i].word2 << dataRowPointer[i].word3 << "\n";
}
return 0;
}
There are 3 strings. I want to separate each individual word within each string and store them into a structure. I have an array of structures to store them. The size of the array is 3 (since there are 3 lines). I do not set the pointer in main, I set the pointer in my function. From there I start collecting my words to store.
I obtain this output:
(blank line)
(blank line)
OhBoy,Hello
My question is, where did my first two structures go?
Your getRow is re-allocating the DataRow array on each invocation, and thus you're losing the results of the first two invocations. Move the allocation into your main().