You treat them as individual characters, or treat them collectively as a string.
Then you process each character at a time:
1. convert it to a number
2. print the character (or number)
3. use the number to determine how many * to print
4. write the new line character
First use While loop (what should be the condition?)
while (temp ...){ // you have to iterate until temp = temp / 10 does not leave behind any quotient
Use a temporary variable and assign number to it (don't think it's necessary in this case but why not?
temp = number;
Use the following logic:
digit = temp % 10; // gets the last digit which would be 2 in your example
temp = temp / 10; // makes sure 2 is omitted for next iteration
cout << digit;
for() // what should be here?
cout<<'*';
cout<<'\n';
Try fixing the missing bits and let us know if you didn't get it ;)
This is what I have so far. I didn't really understand what you guys were saying, so I just brute forced it I think. However I need to output a "?" when a non number is entered as well as the non number itself.
for example
Input:11f9
Output
9 *********
f ?
1 *
1 *
I don't know how to do this because of the way i did it.
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
//Declare all Variables Here
unsigned short usrnum, num, first, second, third, last;
//Input or initialize values Here
cout<<"Create a histogram chart."<<endl;
cout<<"Input 4 digits as characters."<<endl;
cin>>usrnum;
//Histogram Here
first=usrnum/1000;
last=usrnum%10;
third=usrnum%100-last;
second=usrnum%1000-third-last;
switch (last){
case 0:
cout<<"0"<<endl;
break;
case 1:
cout<<"1 *"<<endl;
break;
case 2:
cout<<"2 **"<<endl;
break;
case 3:
cout<<"3 ***"<<endl;
break;
case 4:
cout<<"4 ****"<<endl;
break;
case 5:
cout<<"5 *****"<<endl;
break;
case 6:
cout<<"6 ******"<<endl;
break;
case 7:
cout<<"7 *******"<<endl;
break;
case 8:
cout<<"8 ********"<<endl;
break;
case 9:
cout<<"9 *********"<<endl;
break;
default:
cout<<"?"<<endl;
break;
}
switch (third){
case 0:
cout<<"0"<<endl;
break;
case 10:
cout<<"1 *"<<endl;
break;
case 20:
cout<<"2 **"<<endl;
break;
case 30:
cout<<"3 ***"<<endl;
break;
case 40:
cout<<"4 ****"<<endl;
break;
case 50:
cout<<"5 *****"<<endl;
break;
case 60:
cout<<"6 ******"<<endl;
break;
case 70:
cout<<"7 *******"<<endl;
break;
case 80:
cout<<"8 ********"<<endl;
break;
case 90:
cout<<"9 *********"<<endl;
break;
default:
cout<<"?"<<endl;
break;
}
switch (second){
case 0:
cout<<"0"<<endl;
break;
case 100:
cout<<"1 *"<<endl;
break;
case 200:
cout<<"2 **"<<endl;
break;
case 300:
cout<<"3 ***"<<endl;
break;
case 400:
cout<<"4 ****"<<endl;
break;
case 500:
cout<<"5 *****"<<endl;
break;
case 600:
cout<<"6 ******"<<endl;
break;
case 700:
cout<<"7 *******"<<endl;
break;
case 800:
cout<<"8 ********"<<endl;
break;
case 900:
cout<<"9 *********"<<endl;
break;
default:
cout<<"?"<<endl;
break;
}
switch (first){
case 0:
cout<<"0"<<endl;
break;
case 1:
cout<<"1 *"<<endl;
break;
case 2:
cout<<"2 **"<<endl;
break;
case 3:
cout<<"3 ***"<<endl;
break;
case 4:
cout<<"4 ****"<<endl;
break;
case 5:
cout<<"5 *****"<<endl;
break;
case 6:
cout<<"6 ******"<<endl;
break;
case 7:
cout<<"7 *******"<<endl;
break;
case 8:
cout<<"8 ********"<<endl;
break;
case 9:
cout<<"9 *********"<<endl;
break;
default:
cout<<" ?"<<endl;
break;
}
return 0;
}
#include <iostream>
int main()
{
int number ;
std::cout << "enter a positive number: " ;
std::cin >> number ;
if( number > 0 ) // sanity check
{
// for each digit in the number, from right to left
while( number > 0 )
{
constint digit = number%10 ; // right most digit
number /= 10 ; // the number after removing the right most digit
std::cout << digit << ' ' ; // print the digit
// print as many asterisks as the value of the digit
for( int i = 0 ; i < digit ; ++i ) std::cout << '*' ;
std::cout << '\n' ; // and a new line at the end
}
}
}
sorry, but I don't really understand how yours works. Could you explain how you get it to display all of the digits. Also how would I display a "s ?" if s was entered and if 0 is entered how could I make that display? I tried adding another if statement as well as a while but had no luck.
#include <iostream>
#include <sstream>
#include <string>
int main()
{
std::string number ;
std::cout << "enter a positive number: " ;
std::cin >> number ;
std::stringstream sstr(number);
int zahl;
sstr >> zahl;
if( zahl > 0 ) // sanity check
{
// for each digit in the number, from right to left
while( zahl > 0 )
{
constint digit = zahl%10 ; // right most digit
zahl /= 10 ; // the number after removing the right most digit
std::cout << digit << ' ' ; // print the digit
// print as many asterisks as the value of the digit
for( int i = 0 ; i < digit ; ++i ) std::cout << '*' ;
std::cout << '\n' ; // and a new line at the end
}
}
else
{
std::cout << number << "?" << std::endl;
}
}
I added the code of JLBorges with a stringstream.
You need a string to read numbers and letters, if you want to get the number you have to convert the string to an int, thats why i'm using this stringstream
#include <iostream>
#include <string>
#include <cctype>
int main()
{
std::string str ;
std::cout << "enter some alphanumeric characters: " ;
std::cin >> str ;
constint nchars = str.size() ; // number of characters in the string
// for each character in the string, from right to left
// note: the rightmost character is at position nchars-1
for( int i = nchars-1 ; i >= 0 ; --i )
{
constchar ch = str[i] ; // get the character
if( std::isdigit(ch) ) // if it is a decimal digit
{
constint digit = ch - '0' ; // '6' - '0' == 0 etc.
std::cout << digit << ' ' ; // print the digit
// print as many asterisks as the value of the digit
// note that if digit == 0, no asterisks are printed
for( int j = 0 ; j < digit ; ++j ) std::cout << '*' ;
std::cout << '\n' ; // and a new line at the end
}
elseif( std::isalpha(ch) ) // if it is a letter
{
std::cout << ch << " ?\n" ; // print the letter and a question mark
}
}
}