Home > Blockchain >  Why is my code not allowing me to input values into a string variable with other user inputs for oth
Why is my code not allowing me to input values into a string variable with other user inputs for oth

Time:12-15

I am trying to make a program that inserts a phrase into another phrase at a point that the user determines. However, when I try to take the inputs for each parameter, the two phrases, and the place at which the other phrase needs to be inserted, I am only able to give input for the first parameter asked and then the rest of the code is executed without the input of the two other parameters, and I am unsure why this happens in my code. My code is attached below.

#include <iostream>
#include <string>
#include <cstring>

using namespace std;


int main() {
    string mystr; // original statement
    string substrToBeInserted; // statement to be put into the original statement
    int positionToInsertAfter; //interger value in the string array for where the other statement needs to be put

    cout << endl << "Please enter your statement" << endl;
    cin >> mystr; 
    cout << endl << "Please enter your statement to be inserted" << endl;
    cin >> substrToBeInserted;
    cout << endl << "Please enter where your statement is going to be inserted" << endl;
    cin >> positionToInsertAfter;

    mystr = mystr   mystr[positionToInsertAfter]   substrToBeInserted;
    cout << mystr;

    return 0;
}

Thank you so much for any help! :)

CodePudding user response:

I'm guessing since you intend your first input to be a statement, it's going to have spaces.

standard input operator cin >> mystr will copy till whitespace or newline use getline(cin, mystr) instead.

P.S. your code is going to print whole mystr, a character of mystr according to your index, and substrToBeInserted. Not sure if that's what you want your code to do. string has nice insert sub operation mystr.insert(position, substr) to insert before index position.

(sorry, can't comment yet)

CodePudding user response:

If you want to read a string that contains spaces, you cannot use formatted input functions using operator >>, because this operator will stop reading, if it sees the first space.

So, if you try to read "Hello World Hi", then it will read only "Hello". the word "World" will be read in your next statement, so "substrToBeInserted" will contain "World". cin >> positionToInsertAfter;Will fail completely, becuase it tries to convert "Hi" to an integer number.

Solution: You need to use the function getline to read a complete line of text, including spaces.

Then, the string type offers a function insert, which you can simply use.

One of many possible solutions is:

#include <iostream>
#include <string>
#include <cstring>

using namespace std;


int main() {
    string mystr; // original statement
    string substrToBeInserted; // statement to be put into the original statement
    unsigned int positionToInsertAfter; //interger value in the string array for where the other statement needs to be put

    cout << endl << "Please enter your statement" << endl;
    getline(cin, mystr);
    cout << endl << "Please enter your statement to be inserted" << endl;
    getline(cin,substrToBeInserted);
    cout << endl << "Please enter where your statement is going to be inserted" << endl;
    cin >> positionToInsertAfter;

    if (positionToInsertAfter >= mystr.length())
        positionToInsertAfter = mystr.length();
    mystr.insert(positionToInsertAfter, substrToBeInserted);
    cout << mystr;

    return 0;
}

  • Related