Letters, numbers and print numbers on the screen

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 9
Reputation: dr.eu is an unknown quantity at this point 
Solved Threads: 0
dr.eu dr.eu is offline Offline
Newbie Poster

Letters, numbers and print numbers on the screen

 
0
  #1
Dec 15th, 2008
Hi!

I have to write program, that reads signs and it counts, how many is capital letters, how many small letters, and how many remaining signs. It stops, when reads thrue 20 signs or when sign q or Q is entered. Before the end it writes out typed and calculated numbers.

I have some problems with while loop, it doesn't stops by sign q or Q (that is why I put it in comments) and another, bigger problem is with printing all entered numbers on the screen. Do I have to write entered numbers in array or what?

Thanks for your answers!

dr.eu


  1. #include <iostream.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6. char sign;
  7. int countersmall, countercapital, counterremain, number, counternumbers, sum;
  8.  
  9. countersmall = countercapital = counternumbers = sum = 0;
  10.  
  11. cout << endl << " Program, that reads signs and it counts, how many is capital letters," << endl;
  12. cout << " how many small letters, and how many remaining signs." << endl;
  13. cout << " It stops, when reads thrue 20 signs or when sign q or Q is entered." << endl;
  14. cout << " Before the end it writes out typed and calculated numbers." << endl << endl << endl << endl;
  15. /*
  16.   cout << " Enter sign (q or Q for end) and press ENTER: ";
  17.   cin >> sign;
  18.  
  19.   if(sign == 'q' || sign == 'Q')
  20.   {
  21.   cout << endl << endl << endl << " ***** END ***** " << endl;
  22.   cout << endl << endl << endl;
  23.   system("pause");
  24.   return 0;
  25.   }
  26.  
  27.   else if(sign != 'q' || sign != 'Q')
  28.  
  29.   do
  30.   {
  31. */
  32. for(int counter = 1; counter <= 20; counter++)
  33. {
  34. cout << endl << " Enter " << counter << ". sign (q or Q for end) and press ENTER: ";
  35. cin >> sign;
  36.  
  37.  
  38. if(sign >= 'A' && sign <='Z')
  39. {
  40. countercapital++;
  41. }
  42.  
  43. if(sign >= '0' && sign <='9')
  44. {
  45. counternumbers++;
  46.  
  47. if(sign=='0')
  48. {
  49. number=0;
  50. }
  51.  
  52. else if(sign=='1')
  53. {
  54. number=1;
  55. }
  56.  
  57. else if(sign=='2')
  58. {
  59. number=2;
  60. }
  61.  
  62. else if(sign=='3')
  63. {
  64. number=3;
  65. }
  66.  
  67. else if(sign=='4')
  68. {
  69. number=4;
  70. }
  71.  
  72. else if(sign=='5')
  73. {
  74. number=5;
  75. }
  76.  
  77. else if(sign=='6')
  78. {
  79. number=6;
  80. }
  81.  
  82. else if(sign=='7')
  83. {
  84. number=7;
  85. }
  86.  
  87. else if(sign=='8')
  88. {
  89. number=8;
  90. }
  91.  
  92. else if(sign=='9')
  93. {
  94. number=9;
  95. }
  96.  
  97. else
  98. number=number;
  99. }
  100.  
  101. if(sign >= 'a' && sign <='z')
  102. {
  103. countersmall++;
  104. }
  105.  
  106. sum = sum + number;
  107.  
  108. counterremain = 20 - (counternumbers + countersmall + countercapital);
  109. }
  110.  
  111. cout << endl << endl << endl << endl << " Number of small letters is: " << countersmall;
  112. cout << endl << " Number of capital letters is: " << countercapital;
  113. cout << endl << " Number of remaining signs is: " << counterremain;
  114. cout << endl << " Number of numbers is: " << counternumbers;
  115. cout << endl << " Sum of all numbers is: " << sum;
  116. cout << endl << " Following numbers were entered:" << endl << " ";
  117.  
  118. for(counternumbers = 1; counternumbers <= 19; counternumbers++)
  119. {
  120. cout << number << ", ";
  121. }
  122. cout << number;
  123.  
  124.  
  125. //}while(sign != 'q' || sign != 'Q');
  126.  
  127. cout << endl << endl << endl << " ***** END ***** " << endl;
  128.  
  129. cout << endl << endl << endl << " ";
  130. system("pause");
  131.  
  132. return 0;
  133. }
Last edited by Ancient Dragon; Dec 15th, 2008 at 8:25 am. Reason: add line numbers
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,639
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1497
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Letters, numbers and print numbers on the screen

 
0
  #2
Dec 15th, 2008
Move lines 19-25 down within the loop to line 36 and it should work.

lines 47-99: You don't need to do that at all -- just save the sign in a char array so that they can be printed later at the end of the program. Since sign is a single character I guess you only enter single digit numbers.
Last edited by Ancient Dragon; Dec 15th, 2008 at 8:33 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 9
Reputation: dr.eu is an unknown quantity at this point 
Solved Threads: 0
dr.eu dr.eu is offline Offline
Newbie Poster

Re: Letters, numbers and print numbers on the screen

 
0
  #3
Dec 15th, 2008
OK, I used array char digit[19], but it dont work well, all entered numbers are printed but also some other signs, who fill the rest of array place. Also the sum is wrong too.

  1. #include <iostream.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6. char sign;
  7. int countersmall, countercapital, counterremain, number, counternumbers, sum;
  8.  
  9. char digit[19];
  10.  
  11. countersmall = countercapital = counternumbers = sum = 0;
  12.  
  13. cout << endl << " Program, that reads signs and it counts, how many is capital letters," << endl;
  14. cout << " how many small letters, and how many remaining signs." << endl;
  15. cout << " It stops, when reads thrue 20 signs or when sign q or Q is entered." << endl;
  16. cout << " Before the end it writes out typed and calculated numbers." << endl << endl << endl << endl;
  17.  
  18. do
  19. {
  20. for(int counter = 1; counter <= 20; counter++)
  21. {
  22. cout << " Enter " << counter << ". sign (q or Q for end) and press ENTER: ";
  23. cin >> sign;
  24.  
  25.  
  26. if(sign == 'q' || sign == 'Q')
  27. {
  28. cout << endl << endl << endl << " ***** END ***** " << endl;
  29. cout << endl << endl << endl;
  30. system("pause");
  31. return 0;
  32. }
  33.  
  34.  
  35. else if(sign >= 'A' && sign <='Z')
  36. {
  37. countercapital++;
  38. }
  39.  
  40. else if(sign >= '0' && sign <='9')
  41. {
  42. counternumbers++;
  43.  
  44. digit[counternumbers] = sign;
  45.  
  46. sum = digit[counternumbers];
  47. }
  48.  
  49. else if(sign >= 'a' && sign <='z')
  50. {
  51. countersmall++;
  52. }
  53.  
  54. counterremain = 20 - (counternumbers + countersmall + countercapital);
  55. }
  56.  
  57. sum = sum + digit[counternumbers];
  58.  
  59.  
  60. cout << endl << endl << endl << endl << " Number of small letters is: " << countersmall;
  61. cout << endl << " Number of capital letters is: " << countercapital;
  62. cout << endl << " Number of remaining signs is: " << counterremain;
  63. cout << endl << " Number of numbers is: " << counternumbers;
  64. cout << endl << " Sum of all numbers is: " << sum;
  65. cout << endl << " Following numbers were entered:" << endl << " ";
  66.  
  67. for(counternumbers = 1; counternumbers <= 19; counternumbers++)
  68. {
  69. cout << digit[counternumbers] << ", ";
  70. }
  71. cout << digit[counternumbers];
  72.  
  73. cout << endl << endl;
  74.  
  75. }while(sign != 'q' || sign != 'Q');
  76.  
  77. cout << endl << endl << endl << " ***** END ***** " << endl;
  78.  
  79. cout << endl << endl << endl << " ";
  80. system("pause");
  81.  
  82. return 0;
  83. }

dr.eu
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Letters, numbers and print numbers on the screen

 
0
  #4
Dec 15th, 2008
  1. for(counternumbers = 1; counternumbers <= 19; counternumbers++){
  2. cout << digit[counternumbers] << ", ";
  3. }
  4. cout << digit[counternumbers];

First things first, Arrays are 0 index'ed. This means the first element within the array is 0 not 1. That is your first mistake. Number 2, you have an array of size 19 thus that is 0..18. using the <= operator you print number 1..19 and then you go on and print another character outside of the loop. Since the memory at point digit[19] and upwards isn't controlled by you, you are accessing random data, thus printing out what is junk. You should always be careful with thinks like this.

Change it to
  1. for(counternumbers = 0; counternumbers < 19; counternumbers++){
  2. cout << digit[counternumbers] << ", ";
  3. }

You may have other problems i have meerly address that one!

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 9
Reputation: dr.eu is an unknown quantity at this point 
Solved Threads: 0
dr.eu dr.eu is offline Offline
Newbie Poster

Re: Letters, numbers and print numbers on the screen

 
0
  #5
Dec 15th, 2008
Well, I increase array size to 21, but the output is the same, not only entered numbers. Also the sum of those numbers is not corect.

dr.eu
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Letters, numbers and print numbers on the screen

 
0
  #6
Dec 15th, 2008
Try initialising the array to 0?
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 9
Reputation: dr.eu is an unknown quantity at this point 
Solved Threads: 0
dr.eu dr.eu is offline Offline
Newbie Poster

Re: Letters, numbers and print numbers on the screen

 
0
  #7
Dec 15th, 2008
No, with 0 size of array is not better.
Last edited by dr.eu; Dec 15th, 2008 at 1:21 pm. Reason: mistake
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,639
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1497
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Letters, numbers and print numbers on the screen

 
0
  #8
Dec 15th, 2008
>> sum = digit[counternumbers];
Two problems:
1) use += operator, not =

2)That is adding the ascii value of the digits. You need to convert to integer by subtracting '0', like this: sum += digit[counternumbers] - '0';
Last edited by Ancient Dragon; Dec 15th, 2008 at 1:30 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 9
Reputation: dr.eu is an unknown quantity at this point 
Solved Threads: 0
dr.eu dr.eu is offline Offline
Newbie Poster

Re: Letters, numbers and print numbers on the screen

 
0
  #9
Dec 15th, 2008
Super, Ancient Dragon!

You solved two of my problems, while loop and sum of numbers!!!
It's only one left, the output of entered numbers:
for example, if user entered numbers 1 2 3 4 5 6 7 8 9 and 0, the output must be: 1,2,3,4,5,6,7,8,9,0.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,639
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1497
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Letters, numbers and print numbers on the screen

 
0
  #10
Dec 15th, 2008
what you have should work -- but move it down outside that loop. You are getting strange charcters because it is trying to display uninitialized array elements which have not been filled yet.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 739 | Replies: 10
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC