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

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




reading integer values

 
Reply to this topicStart new topic

reading integer values

svoryan
1 Dec, 2007 - 06:42 AM
Post #1

New D.I.C Head
*

Joined: 20 May, 2007
Posts: 17


My Contributions
My program should take a user inputed paragraph and check for end of sentence marker(i.e. periods, exclamations and question marks), intrasentence markers(i.e. commas, colons, semicolons), blank spaces and digits. Once it checks for these things, it should add up the number of times these things were used and them cout them to the user. My problem is that i cant figure out how to read in the digits or integers and then add them up.
I tried using an if statement but that doesn't work and im sure their is more to it. Any help would be much appreciated.



[//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;
}
if (para > 0 || para < 0 || para == 0)
{
dig++;
}
}
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);
}



}]
User is offlineProfile CardPM
+Quote Post

HKothari
RE: Reading Integer Values
1 Dec, 2007 - 07:13 AM
Post #2

New D.I.C Head
*

Joined: 1 Dec, 2007
Posts: 12


My Contributions
Wait, so you want it to count up like the number of exclamations, question marks, etc.?
User is offlineProfile CardPM
+Quote Post

svoryan
RE: Reading Integer Values
1 Dec, 2007 - 07:41 AM
Post #3

New D.I.C Head
*

Joined: 20 May, 2007
Posts: 17


My Contributions
Yes exactly, and it does all that just fine. My problem is when it comes to integers. I dont know how to find or count them in the inputed paragraph. And by count, i dont mean count their actual values, i mean increment by one every time an integer is encountered.
User is offlineProfile CardPM
+Quote Post

HKothari
RE: Reading Integer Values
1 Dec, 2007 - 07:43 AM
Post #4

New D.I.C Head
*

Joined: 1 Dec, 2007
Posts: 12


My Contributions
There are multiple paragraphs, right?

This post has been edited by HKothari: 1 Dec, 2007 - 07:44 AM
User is offlineProfile CardPM
+Quote Post

svoryan
RE: Reading Integer Values
1 Dec, 2007 - 08:08 AM
Post #5

New D.I.C Head
*

Joined: 20 May, 2007
Posts: 17


My Contributions
No, only one paragraph because the the program quits reading when it encounters enter('\n'). Therefore, the user could only enter one paragraph.

User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Reading Integer Values
1 Dec, 2007 - 08:14 AM
Post #6

 101010101
Group Icon

Joined: 3 Aug, 2006
Posts: 1,812



Thanked: 1 times
Dream Kudos: 755
My Contributions
For the case of integers you should use the condition:
CODE

if (para >= '0' || para <='9')
//rest of code

Remember para is a char not an int.
User is offlineProfile CardPM
+Quote Post

svoryan
RE: Reading Integer Values
1 Dec, 2007 - 08:39 AM
Post #7

New D.I.C Head
*

Joined: 20 May, 2007
Posts: 17


My Contributions
Well i tried that and instead of just couting the integers, it counted every single thing inputed. I wrote a paragraph with a sum of 18 letters, spaces, integers etc.. and it outputted 18 for the integer values....... Any ideas?

here is the new code if you want to run it

[#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;
}
if (para >= '0' || para <='9')
{
dig++;
}
}
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);
}



}]
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Reading Integer Values
1 Dec, 2007 - 09:45 AM
Post #8

 101010101
Group Icon

Joined: 3 Aug, 2006
Posts: 1,812



Thanked: 1 times
Dream Kudos: 755
My Contributions
Oops! My bad. The condition should be an AND not and OR like so:
CODE

if (para >= '0' && para <='9')


An OR will count everything except integers. Sorry.
Hope this helps smile.gif
User is offlineProfile CardPM
+Quote Post

svoryan
RE: Reading Integer Values
1 Dec, 2007 - 10:02 AM
Post #9

New D.I.C Head
*

Joined: 20 May, 2007
Posts: 17


My Contributions
works beautifully, appreciate the help, i owe ya.
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Reading Integer Values
1 Dec, 2007 - 10:05 AM
Post #10

 101010101
Group Icon

Joined: 3 Aug, 2006
Posts: 1,812



Thanked: 1 times
Dream Kudos: 755
My Contributions
Glad I could help smile.gif!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/9/09 06:01AM

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