Question:
C++ programming help, skips input string?
Erick
2010-04-26 16:59:13 UTC
the first time the program is executed it runs fine
after the user inputs a key to continue it displays
#1,#2, AND #3 together and asks the user to input a key to continue

it skips the first cout

also, how do i do the charecter count without the spaces, and the word count?

*******************************************
#include
#include

int main ( )
{
char choice = ' ';
std::string sentence = " ";



do {
//#1
std::cout << "Please enter your sentence, ending with a period(.): " << std::flush;
std::getline (std::cin, sentence);

if (sentence.length() > 100 || sentence.substr(sentence.length() - 1, 1) != ".")
{

std::cout << "Error missing '.' or exceeded number of characters" << std::endl;

}


else
{
//#2
std::cout << "Your sentence has... " << std::endl;
std::cout << "Words: " << std::endl;
std::cout << "Characters (no spaces): " << std::endl;
std::cout << "Characters (with spaces): " << sentence.length() << std::endl;
std::cout << "The letter 'a'|'A' count: " << sentence.find('a', 0) << std::endl;
}
//#3
std::cout << "Press any key to continue...('s' to stop): " << std::flush;
std::cin >> choice;

} while (choice != 's');

return (0);//end

}
Four answers:
Charlie Tuna
2010-04-26 17:52:21 UTC
It should work now. I made a lot of changes, so I'm including the whole program here. The letter A|a count is still unresolved. As it is now, it will only return the first occurrence of the letter a, and it will give you a weird number if you only enter the "." as input. I'll let you fix that.

You did not specify if you wanted to include the period (.) in the word count, I assumed not, but included comments to make the program count the period.



Let me know if you need more help. Tuna.





#include

#include



using namespace std;

int main ( )

{

string choice = " ";

string sentence = " ";



do {

//#1

cout << "\nPlease enter your sentence, ending with a period(.): " << flush;

getline (cin, sentence);



if (sentence.length() > 100 || sentence.substr(sentence.length() - 1, 1) != ".")

cout << "Error missing '.' or exceeded number of characters" << endl;

else

{

//#2

// Count words

int sentlength = sentence.length();

int charct =0; // Character count

int wordct = 0; // Word count



// for (int i = 1; i <= sentlength; i++) // ---> Uncomment this line and comment the next line to count the period as part of the sentence.

for (int i = 1; i < sentlength; i++)

{

if (sentence.substr(i, 1) != " ")

charct++;

else

wordct++;

}



cout << "Your sentence has ..." << endl;

cout << "Words: " << wordct + 1 << endl;

cout << "Characters (no spaces): " << charct << endl;

// cout << "Characters (with spaces): " << sentence.length() << endl; // ---> Uncomment this line and comment the next line to count the period as part of the sentence.

cout << "Characters (with spaces): " << sentence.length() - 1 << endl;

cout << "The letter 'a'|'A' count: " << sentence.find('a', 0) << endl;

}

//#3

cout << "Press any key to continue...('s' to stop): ";



getline(cin, choice);



} while (choice != "s");

cout << endl;

return (0);//end



}
cja
2010-04-27 12:22:27 UTC
Use getline to get the response to your "continue?" prompt; cin to get one char leaves the newline in the stream, which will be read when you loop back to the top for the next getline.



See below for my solution, it should give you some ideas for how to complete your program. I didn't bother making sure a period was at the end of the input. I don't know why you're checking for entered strings > 100 characters. I didn't see a need for that, so I left that out as well.



#include

#include

#include

#include

#include



using namespace std;



int wordCount(const string&);

int ciCharCount(const string&, char);

char myTolower(char c);



int main(int argc, char *argv[]) {

    string line;

    int spaces;



    do {

        cout << "> ";

        getline(cin,line);

        cout << endl << "words: " << wordCount(line) << endl;

        cout << "spaces: " << (spaces = ciCharCount(line,' ')) << endl;

        cout << "total characters: " << line.size() << endl;

        cout << "non-space characters: " << line.size()-spaces << endl;

        cout << "'A|a' : " << ciCharCount(line,'a') << endl;

        cout << endl << "Continue (y/n)? : ";

        getline(cin,line);

    } while (tolower(line[0]) != 'n');

    return(0);

}



int wordCount(const string &s) {

    string word;

    stringstream ss(s);

    int wordCount = 0;



    while (ss.good()) {

        if (ss >> word) ++wordCount;

    }

    return wordCount;

}



//

// Case ignoring character counter

//

int ciCharCount(const string &s, char c) {

    size_t pos = 0;

    int cnt = 0;

    char findChar = tolower(c);

    string lcStr(s);



    transform (lcStr.begin(), lcStr.end(), lcStr.begin(), myTolower);

    while ((pos = lcStr.find(findChar,pos)) != string::npos) {

        ++cnt;

        ++pos;

    }

    return cnt;

}



char myTolower(char c) {

    return static_cast(tolower(c));

}





#if 0



> A sentence with several words in it.



words: 7

spaces: 6

total characters: 36

non-space characters: 30

'A|a' : 2



Continue (y/n)? : y

> goodbye



words: 1

spaces: 0

total characters: 7

non-space characters: 7

'A|a' : 0



Continue (y/n)? : n



#endif
Mike
2010-04-26 17:19:59 UTC
The problem is this line

std::getline (std::cin, sentence);

changed to std::cin >> sentence;



Solution:

#include

#include



int main ( )

{

char choice = ' ';

std::string sentence = " ";







do {

//#1

std::cout << "Please enter your sentence, ending with a period(.): " << std::flush;

std::cin >> sentence;

//std::getline (std::cin, sentence);



if (sentence.length() > 100 || sentence.substr(sentence.length() - 1, 1) != ".")

{



std::cout << "Error missing '.' or exceeded number of characters" << std::endl;



}





else

{

//#2

std::cout << "Your sentence has... " << std::endl;

std::cout << "Words: " << std::endl;

std::cout << "Characters (no spaces): " << std::endl;

std::cout << "Characters (with spaces): " << sentence.length() << std::endl;

std::cout << "The letter 'a'|'A' count: " << sentence.find('a', 0) << std::endl;

}

//#3

std::cout << "Press any key to continue...('s' to stop): " << std::flush;

std::cin >> choice;



} while (choice != 's');



return (0);//end



}
2016-11-02 01:06:50 UTC
it is the variety you ought to enforce this: #comprise #comprise #comprise #comprise typedef union{ int iNumber; char buffer[256]; } enter; void get_input(enter *p) { p->iNumber = 0; *p->buffer = 0; char buffer[256]; fgets(buffer, sizeof(buffer), stdin); if (isdigit(*p->buffer)) sscanf(buffer, "%d", &p->iNumber); else strcpy(p->buffer, buffer); } int substantial() { enter p; printf("String or variety> "); get_input(&p); if (*p.buffer) printf("%s", p.buffer); else printf("%d", p.iNumber); return 0; }


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...