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

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




Switch problem

 
Reply to this topicStart new topic

Switch problem

svoryan
30 Nov, 2007 - 02:04 PM
Post #1

New D.I.C Head
*

Joined: 20 May, 2007
Posts: 17


My Contributions
Here is the code,


CODE
//This program will recieve user input in the form of a paragraph and
//count the number of end sentence markers, intrasentence markers, number of spaces
//and the number of digits. It will then output this information to the user.

#include <iostream>
#include <limits>
using namespace std;

void welcome();
void getYorn(char&);

int main()
{
            

            char para;                            
            int numUpper = 0;                          
            char yorn;                        
            int smark = 0;
            int imark = 0;
            int blank = 0;
            int dig = 0;
            int sum = 0;

            welcome();
            getYorn(yorn);
            
            while (tolower(yorn) == 'y')
            {
                        cout << "Please enter a paragraph pressing enter when done: ";
                        cin.sync();              
                        for (cin.get(para); para != '\n'; cin.get(para))
                        {
                            switch (para)
                            {
                            case '.':
                            case '!':
                            case '?': smark++;
                                break;
                            case ',':
                            case ';':
                            case ':': imark++;
                                break;
                            case ' ': blank++;
                                break;
                            case : dig++;
                                break;
                            }
                        }
                        cout << endl;
                        cout << "The number of end of sentence markers is: " << smark << endl;
                        cout << "The number of intrasentence markers is: " << imark << endl;
                        cout << "The number of spaces is: " << blank << endl;
                        cout << "The number of digits is: " << dig << endl;
                        getYorn(yorn);
            
                        smark = 0;
                        imark = 0;
                        blank = 0;
                        dig = 0;
            }
            cout << "Thank you - goodbye!" << endl;
            return 0;
}
            

void welcome()
{
    cout << "Welcome to Ryan's paragraph processing information system." << endl;
    cout << "This program will calculate the number of end sentence markers, " << endl;
    cout << "intrasentence markers, number of spaces and number of digits within " << endl;
    cout << "a paragraph. " << endl << endl;

    
}
void getYorn(char& yorn)
{
    cout << endl;
    cout << "Do you wish to continue? (y = yes, n = no) : ";
                        cin.sync();
                        cin.get(yorn);
                        while (tolower(yorn) != 'y' && tolower(yorn) != 'n')
                        {
                        cout << endl;
                        cout << "Invalid response. Do you wish to continue? ";
                                   cin.sync();
                                   cin.clear();
                                   cin.get(yorn);
                        }
                      
    
      
}


The program works great accept for the digit counting part(variable dig), which is the last case in the switch statement. Im not sure how i can identify and count in integers while reading a users inputted paragraph. Any Help would be much appreciated.

Use code.gif tags
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Switch Problem
30 Nov, 2007 - 02:20 PM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
do you mean count integer text?
You seem to understand switch statements well enough. isn't
CODE

case '1':
case '2':
//....

all you are looking for? or is there more? if so you can call methods and functions from a switch case, which you could set the default to check for integers.,
User is offlineProfile CardPM
+Quote Post

svoryan
RE: Switch Problem
30 Nov, 2007 - 03:39 PM
Post #3

New D.I.C Head
*

Joined: 20 May, 2007
Posts: 17


My Contributions
Im quite sure i follow you so i will be as clear as possible to my problem. Say the user enters the following paragraph.
"Hello my name is 7?" The program should output 1 for the question mark, and 1 for the # 7 since there is 1 end of sentence marker(i.e. ?) and 1 digit(i.e. 7) found in the paragraph. My problem is that im not sure what to put in the last case so that it recognizes and adds digits found in the users inputed paragraph. Hopefully this explanation is more clear to my exact problem.
User is offlineProfile CardPM
+Quote Post

curiose
RE: Switch Problem
1 Dec, 2007 - 11:27 AM
Post #4

D.I.C Head
**

Joined: 28 Oct, 2007
Posts: 94


My Contributions
hello .... i think that u r faceing a problem in the output in the switch case it self as u have explained...add ur output after each case...and by the way in switch case we dont use (') with numbers in the case but only with characters.....
User is offlineProfile CardPM
+Quote Post

Bench
RE: Switch Problem
1 Dec, 2007 - 02:33 PM
Post #5

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 686



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(curiose @ 1 Dec, 2007 - 07:27 PM) *

hello .... i think that u r faceing a problem in the output in the switch case it self as u have explained...add ur output after each case...and by the way in switch case we dont use (') with numbers in the case but only with characters.....

Although characters do also represent numerical digits. In this case, if the user types in 7, and his program retrieves a character, the value which it will retrieve will be the character '7' and not the number 7.


To the OP - One option would be to use the default case to check. the standard library comes equipped with a function called isdigit which can tell whether or not a character is a numerical digit.

Try something like this -
CODE
    switch (para)
    {
    case '.':
    case '!':
    case '?': smark++;
        break;
    case ',':
    case ';':
    case ':': imark++;
        break;
    case ' ': blank++;
        break;
    default :
        if( isdigit(para) )
            dig++;

    }


Edit - in order to use isdigit, you will need to add the line #include <cctype> near the top of your program.

This post has been edited by Bench: 1 Dec, 2007 - 02:39 PM
User is offlineProfile CardPM
+Quote Post

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

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