| | |
Letters, numbers and print numbers on the screen
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2008
Posts: 9
Reputation:
Solved Threads: 0
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
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
C++ Syntax (Toggle Plain Text)
#include <iostream.h> #include <stdlib.h> int main() { char sign; int countersmall, countercapital, counterremain, number, counternumbers, sum; countersmall = countercapital = counternumbers = sum = 0; cout << endl << " Program, that reads signs and it counts, how many is capital letters," << endl; cout << " how many small letters, and how many remaining signs." << endl; cout << " It stops, when reads thrue 20 signs or when sign q or Q is entered." << endl; cout << " Before the end it writes out typed and calculated numbers." << endl << endl << endl << endl; /* cout << " Enter sign (q or Q for end) and press ENTER: "; cin >> sign; if(sign == 'q' || sign == 'Q') { cout << endl << endl << endl << " ***** END ***** " << endl; cout << endl << endl << endl; system("pause"); return 0; } else if(sign != 'q' || sign != 'Q') do { */ for(int counter = 1; counter <= 20; counter++) { cout << endl << " Enter " << counter << ". sign (q or Q for end) and press ENTER: "; cin >> sign; if(sign >= 'A' && sign <='Z') { countercapital++; } if(sign >= '0' && sign <='9') { counternumbers++; if(sign=='0') { number=0; } else if(sign=='1') { number=1; } else if(sign=='2') { number=2; } else if(sign=='3') { number=3; } else if(sign=='4') { number=4; } else if(sign=='5') { number=5; } else if(sign=='6') { number=6; } else if(sign=='7') { number=7; } else if(sign=='8') { number=8; } else if(sign=='9') { number=9; } else number=number; } if(sign >= 'a' && sign <='z') { countersmall++; } sum = sum + number; counterremain = 20 - (counternumbers + countersmall + countercapital); } cout << endl << endl << endl << endl << " Number of small letters is: " << countersmall; cout << endl << " Number of capital letters is: " << countercapital; cout << endl << " Number of remaining signs is: " << counterremain; cout << endl << " Number of numbers is: " << counternumbers; cout << endl << " Sum of all numbers is: " << sum; cout << endl << " Following numbers were entered:" << endl << " "; for(counternumbers = 1; counternumbers <= 19; counternumbers++) { cout << number << ", "; } cout << number; //}while(sign != 'q' || sign != 'Q'); cout << endl << endl << endl << " ***** END ***** " << endl; cout << endl << endl << endl << " "; system("pause"); return 0; }
Last edited by Ancient Dragon; Dec 15th, 2008 at 8:25 am. Reason: add line numbers
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.
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.
•
•
Join Date: Dec 2008
Posts: 9
Reputation:
Solved Threads: 0
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.
dr.eu
C++ Syntax (Toggle Plain Text)
#include <iostream.h> #include <stdlib.h> int main() { char sign; int countersmall, countercapital, counterremain, number, counternumbers, sum; char digit[19]; countersmall = countercapital = counternumbers = sum = 0; cout << endl << " Program, that reads signs and it counts, how many is capital letters," << endl; cout << " how many small letters, and how many remaining signs." << endl; cout << " It stops, when reads thrue 20 signs or when sign q or Q is entered." << endl; cout << " Before the end it writes out typed and calculated numbers." << endl << endl << endl << endl; do { for(int counter = 1; counter <= 20; counter++) { cout << " Enter " << counter << ". sign (q or Q for end) and press ENTER: "; cin >> sign; if(sign == 'q' || sign == 'Q') { cout << endl << endl << endl << " ***** END ***** " << endl; cout << endl << endl << endl; system("pause"); return 0; } else if(sign >= 'A' && sign <='Z') { countercapital++; } else if(sign >= '0' && sign <='9') { counternumbers++; digit[counternumbers] = sign; sum = digit[counternumbers]; } else if(sign >= 'a' && sign <='z') { countersmall++; } counterremain = 20 - (counternumbers + countersmall + countercapital); } sum = sum + digit[counternumbers]; cout << endl << endl << endl << endl << " Number of small letters is: " << countersmall; cout << endl << " Number of capital letters is: " << countercapital; cout << endl << " Number of remaining signs is: " << counterremain; cout << endl << " Number of numbers is: " << counternumbers; cout << endl << " Sum of all numbers is: " << sum; cout << endl << " Following numbers were entered:" << endl << " "; for(counternumbers = 1; counternumbers <= 19; counternumbers++) { cout << digit[counternumbers] << ", "; } cout << digit[counternumbers]; cout << endl << endl; }while(sign != 'q' || sign != 'Q'); cout << endl << endl << endl << " ***** END ***** " << endl; cout << endl << endl << endl << " "; system("pause"); return 0; }
dr.eu
C++ Syntax (Toggle Plain Text)
for(counternumbers = 1; counternumbers <= 19; counternumbers++){ cout << digit[counternumbers] << ", "; } 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
C++ Syntax (Toggle Plain Text)
for(counternumbers = 0; counternumbers < 19; counternumbers++){ cout << digit[counternumbers] << ", "; }
You may have other problems i have meerly address that one!
Chris
Knowledge is power -- But experience is everything
>> 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:
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.
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.
![]() |
Similar Threads
- Tutorial: An introduction to algorithms (Computer Science)
- Pointers (archived tutorial) (C++)
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- PayPal? - vs. other online escrow services (eCommerce)
- Tutorials for Linux (*nix Software)
- C++ Syntax (C++)
Other Threads in the C++ Forum
- Previous Thread: Dev ++ Help its Not Working
- Next Thread: programming hw (easy)
Views: 739 | Replies: 10
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






