Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,217 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,160 people online right now. Registration is fast and FREE... Join Now!




Problem using cin.get function

 
Reply to this topicStart new topic

Problem using cin.get function

svoryan
7 Nov, 2007 - 01:20 PM
Post #1

New D.I.C Head
*

Joined: 20 May, 2007
Posts: 17


My Contributions
This is what i have so far. I was told to use the cin.get() function in order to read in a character one at a time in order to count the number of characters inputed. I dont think i used it right and after i input a paragraph the program just freezes up. The parameters of this project are to exclude white spaces from the count and end when the loop reaches the return key. Any help would be greatly appreciated.

CODE

#include<iostream>
#include<string>

using namespace std;

int main()
{
    char para;
    char yorn;
    int counter = 0;

    cout << "Welcome to Ryan's Character Counter program! This program will allow you to" << endl;
    cout << "enter any size paragraph and will return the number of characters in that" << endl;
    cout << "paragraph. You will enter a paragraph and when finished, press enter. At"  << endl;
    cout<< "that time, the program will calculate the number of characters entered, print" << endl;
    cout<< "out that value and ask you if you wish to continue." << endl << endl;
    cout << "Do you wish to continue? (y = yes, n = no): ";
    cin >> yorn;

    yorn = tolower(yorn);

    while (yorn != 'y' && yorn != 'n')
    {
        cout << "Invalid response, do you wish to continue? (y = yes, n = no): ";
        cin.sync();
        cin >> yorn;
    }

    if (yorn == 'n')
        return 0;

    while (yorn == 'y')
    {
        system("cls");

        cout << "Enter the paragraph - ";
        cin.sync();
        cin.clear();
        cin.get(para);
    while (para != '\n')
    {
        if (para != ' ')
        {
             counter += para;
        }
    }
    cout << "The number of nonblank characters is: " << counter << endl;

    return (0);
    }
}

edit: Code placed in code tags - Martyr2

This post has been edited by Martyr2: 7 Nov, 2007 - 02:09 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Problem Using Cin.get Function
7 Nov, 2007 - 02:16 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Your solution is pretty simple, it freezes because you are in an infinite loop. You forgot to pull the next character in the loop so that it will keep changing the value of "para". You also setup your counter wrong. You simply need to add one to it, not attempt to add a character to an integer variable. This is what your loop should look like...

CODE

while (para != '\n')
{
     if (para != ' ')
     {
          // Add one to the counter
          counter += 1;
     }

     // Get our next character and go back to the start of the loop for testing.
     cin.get(para);
}


So after you change these two things, you will see your program working like a charm.

Enjoy!

"At DIC we be character counting code ninjas!" decap.gif
User is offlineProfile CardPM
+Quote Post

svoryan
RE: Problem Using Cin.get Function
7 Nov, 2007 - 02:52 PM
Post #3

New D.I.C Head
*

Joined: 20 May, 2007
Posts: 17


My Contributions

Well I changed the loop and when i input a paragraph and press enter, the entered paragraph deletes and does virtually nothing after that. Here is the new code, Any more suggestions?

[#include<string>
#include<iostream>

using namespace std;

int main()
{
char para;
char yorn;
int counter = 0;

cout << "Welcome to Ryan's Character Counter program! This program will allow you to" << endl;
cout << "enter any size paragraph and will return the number of characters in that" << endl;
cout << "paragraph. You will enter a paragraph and when finished, press enter. At" << endl;
cout<< "that time, the program will calculate the number of characters entered, print" << endl;
cout<< "out that value and ask you if you wish to continue." << endl << endl;
cout << "Do you wish to continue? (y = yes, n = no): ";
cin >> yorn;

yorn = tolower(yorn);

while (yorn != 'y' && yorn != 'n')
{
cout << "Invalid response, do you wish to continue? (y = yes, n = no): ";
cin.sync();
cin >> yorn;
}

if (yorn == 'n')
return 0;

while (yorn == 'y')
{
system("cls");
cout << "Enter the paragraph - ";
cin.sync();
cin.clear();
cin.get(para);

while (para != '\n')
{

if (para != ' ')
{
counter += 1;
}
cin.get(para);
}
}
cout << "The number of nonblank characters is: " << counter << endl;

return (0);

}]
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/9/09 05:32AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month