galeontiger
galeontiger

Reputation: 33

c++ cout string not working. Please advise

I have the below code but for some reason the very last instance (where the noSpaces string should print out) is not being output for whatever reason.

I've already verified that the entire program (excluding that one output string) is working perfectly.

Note: I am speaking about the line right about return 0

#include <string>
#include <cstdlib>
#include <iostream>

using namespace std;

int countChar(string str);
void removeSpaces(string str);


int main()
{
    cout << "Select a word: ";
    string selectedWord;
    getline(cin,selectedWord);
    cout << "The number of characters in the word " << selectedWord << " is " << countChar(selectedWord) << endl;

    int i=0;
    int j=0;
    string noSpaces;
    while(selectedWord[i]!='\0') {
        if(selectedWord[i]!=' ') {
            noSpaces[j]=selectedWord[i];
            j++;
        }
        i++;
    }
    cout << "The reverse of your selected word is: " << noSpaces << endl;

    return 0;
}

int countChar(string str)
{
    int i=0;
    while(str[i]!='\0') {
        i++;
    }
    return i;
}

Upvotes: 0

Views: 1267

Answers (4)

Blacktempel
Blacktempel

Reputation: 3995

This question is tagged C++, so... use it then. Also there's no need to make a second std::string. You don't seem to use the first one again.

#include <string>
//#include <cstdlib>
#include <iostream>


int main(void)
{
    std::cout << "Select a word: " << std::endl;
    std::string selectedWord;
    std::getline(std::cin,selectedWord);
    std::cout << "The number of characters in the word " << selectedWord << " is " << selectedWord.size() << std::endl;

    for (std::string::iterator itr = selectedWord.begin(); itr != selectedWord.end(); ++itr)
    {
        if (*itr == ' ') //0x20
        {
            itr = selectedWord.erase(itr);
        }
    }

    std::cout << "The reverse of your selected word is: " << selectedWord << std::endl;

    return EXIT_SUCCESS;
}

+

noSpaces += selectedWord[i];
//or
noSpaces.push_back(selectedWord[i]);

Your std::string noSpaces; is empty at this place.

Upvotes: 0

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145204

This line,

noSpaces[j]=selectedWord[i];

has Undefined Behavior because the length of nospaces is zero: you're indexing storage that doesn't exist.

Replace with

noSpaces.push_back( selectedWord[i] );

Upvotes: 1

Here is the modified program:

#include <string>
#include <cstdlib>
#include <iostream>

using namespace std;

int countChar(string str);

int main()
{
    cout << "Select a word: ";
    string selectedWord;
    getline(cin, selectedWord);
    cout << "The number of characters in the word " << selectedWord << " is " << countChar(selectedWord) << endl;

    int i = 0;
    string noSpaces= string();
    while (selectedWord[i] != '\0') {
        if (selectedWord[i] != ' ') {
            noSpaces+=selectedWord[i];
        }
        i++;
    }
    cout << "The word without spaces is: " << noSpaces << endl;

    return 0;
}

int countChar(string str)
{
    int i = 0;
    while (str[i] != '\0') {
        i++;
    }
    return i;
}
  1. To append characters to string, use:

    noSpaces+=selectedWord[i];

Upvotes: 0

priyadarshani
priyadarshani

Reputation: 11

you did some few mistakes here,you can use string.lenght(). here is the answer

#include <string>
#include <cstdlib>
#include <iostream>

using namespace std;

int countChar(string str);
void removeSpaces(string str);


int main()
{
    cout << "Select a word: ";
    string selectedWord;

    getline(cin,selectedWord);
    int x= countChar(selectedWord);
    cout << "The number of characters in the word " << selectedWord << " is "  << endl;

    int i=0;
    int j=0;
    string noSpaces;
    selectedWord.end();
    for(int i=selectedWord.length()-1; i>=0; i--){
        if(selectedWord[i]!=' ') {
            char c=selectedWord[i];
            noSpaces+=c;
        }
         j++;
    }
    cout << "The reverse of your selected word is: " << noSpaces << endl;

    return 0;
}

int countChar(string str)
{

    int i=str.length();
   /* while(str[i]!='\0') {
        i++;
    }*/
    return i;
}

Upvotes: 0

Related Questions