690d76ede81cf142565ee77b2f507bb4

hi,

first time here dont be to hard with me...
"challenge":
type in some numbers and then print them this way... 5 *****

regards,
buk

#include <iostream>
using namespace std;


int main () {
	
	int readInNumber;
	int readInStart = 0;
	int readInEnd = 5;
	
	while(readInStart < readInEnd)
	{
		++readInStart;
		
		cout << "Please type in " << readInEnd << " numbers between 1 and 80: " << endl;
		cin >> readInNumber;
		
		if(readInNumber > 0 && readInNumber < 81) {
			cout << " Succeed!" << endl;
		}
	
		if(readInNumber <=0 || readInNumber >= 81) {
			cout << " Uh, wrong number!" << endl;
			--readInStart;
		}
		
		for(int i = 0; i < readInNumber; i++)
			cout << "*" << endl;
	}
	
	return 0;
}

Refactorings

No refactoring yet !

A8d3f35baafdaea851914b17dae9e1fc

Adam

December 19, 2008, December 19, 2008 04:54, permalink

1 rating. Login to rate!
#include <iostream>

int main(int argc, char **argv)
{
    int input;
    const char *messages[] = { "Uh, wrong number!", "Success!" };

    for (int i = 5; i > 0; i--) {
        std::cout << "Please type in " << i << " numbers between 1 and 80: ";
        std::cin  >> input;
        
        std::cout << messages[input >= 1 && input <= 80] << std::endl;
        while (input--) std::cout << "*";            
        std::cout << std::endl;
    }
    
    return 0;
}
55502f40dc8b7c769880b10874abc9d0

hackerii.myopenid.com

December 19, 2008, December 19, 2008 20:17, permalink

1 rating. Login to rate!
12345
30a45c64b2ef288f7ec098c5fb353a22

Nathan

December 20, 2008, December 20, 2008 08:53, permalink

No rating. Login to rate!
#include <iostream>
using namespace std;


int main () 
{	
    int readNumber;
    int readNrs = 0;
    int totalNrs = 5;
    const char numbers[] = { "first","second","third","fourth","fifth" };
	
    
    cout << "Your mission: type in " << totalNrs << " numbers between 1 and 80! " << endl;
    while(readNrs < totalNrs)
    {
        cout << "Please type the " << numbers[readNrs] << " number between 1 and 80: " << endl;
        cin >> readNumber;
		
	if(readNumber > 0 && readNumber <= 80) 
        {
	    cout << " Succeed!" << endl;
            readNrs++;
            for(int i = 0; i < readNumber; i++)
            {
                cout << "*";
            }
            cout << endl;
	}
        else
        {
            cout << " Uh, wrong number!" << endl;
	}		
    }	
    return 0;
}
30a45c64b2ef288f7ec098c5fb353a22

Nathan

December 20, 2008, December 20, 2008 08:59, permalink

1 rating. Login to rate!

[Grrrrrr. Layout-troubles. Mental note: do not mix tabs and spaces. Hope this one works better.]

#include <iostream>
using namespace std;


int main () {
	
    int readNumber;
    int readNrs = 0;
    int totalNrs = 5;
    const char numbers[] = { "first","second","third","fourth","fifth" };
	
    cout << "Please type in " << totalNrs << " numbers between 1 and 80: " << endl; 
    while(readNrs < totalNrs)
    {
        cout << "Please type the " << numbers[readNrs] << " number between 1 and 80: " << endl;
        cin >> readNumber;
		
        if(readNumber > 0 && readNumber <= 80) 
        {
            cout << " Succeed!" << endl;
            readNrs++;
            for(int i = 0; i < readNumber; i++)
            {
                cout << "*";
            }
            cout << endl;
        }
        else
        {
             cout << " Uh, wrong number!" << endl;
        }		
    }	
    return 0;
}
5881adc2132926e016299ccaa37bd179

bryan e

February 5, 2009, February 05, 2009 17:46, permalink

No rating. Login to rate!

I think there's some problems with the initial code (consider the output if I type "-1" as my number - that's a LOT of "*"' printed). This is a rewrite to what I think is intended. It also contains a few code simplifications...

void thine(const int readInEnd, const int maxCharBound)
{
    int readInStart = 1; 

    while (readInStart <= readInEnd ) {

        cout << "Please type in " << readInEnd << " numbers between 1 and " << maxCharBound-1 << ": " << endl;

        int readInNumber;
        cin >> readInNumber;

        if(!(readInNumber > 0 && readInNumber < maxCharBound)) {
            cout << "illegal number: " << readInNumber << endl;
            continue;
        }

        cout << "accepted number: " << readInNumber << endl;
        for(int i = 0; i < readInNumber; i++) cout << "*"; 
        cout << endl;

        readInStart++;
    }    
}

int main() { thine(5,81); }
5881adc2132926e016299ccaa37bd179

bryan e

February 5, 2009, February 05, 2009 17:47, permalink

No rating. Login to rate!

I think there's some problems with the initial code (consider the output if I type "-1" as my number - that's a LOT of "*"' printed). This is a rewrite to what I think is intended. It also contains a few code simplifications...

void thine(const int readInEnd, const int maxCharBound)
{
    int readInStart = 1; 

    while (readInStart <= readInEnd ) {

        cout << "Please type in " << readInEnd << " numbers between 1 and " << maxCharBound-1 << ": " << endl;

        int readInNumber;
        cin >> readInNumber;

        if(!(readInNumber > 0 && readInNumber < maxCharBound)) {
            cout << "illegal number: " << readInNumber << endl;
            continue;
        }

        cout << "accepted number: " << readInNumber << endl;
        for(int i = 0; i < readInNumber; i++) cout << "*"; 
        cout << endl;

        readInStart++;
    }    
}

int main() { thine(5,81); }
5881adc2132926e016299ccaa37bd179

bryan e

February 5, 2009, February 05, 2009 17:49, permalink

No rating. Login to rate!

Here's a refactoring to isolate concerns and make the top-level routine easier to grok. Doesn't have to use exceptions, of course, but it simplifies the code and allows the top-level routine to easily deal with problems.

void showPrompt(const int readInEnd, const int maxCharBound)
{
    cout << "Please type in " << readInEnd << " numbers between 1 and " << maxCharBound-1 << ": " << endl;
}

int getInput(const int maxCharBound)
{
    int readInNumber;
    cin >> readInNumber;
    if(!(readInNumber > 0 && readInNumber < maxCharBound)) throw readInNumber;
    return readInNumber;
}

void showOutput(const int readInNumber)
{
    for (int i = 0; i < readInNumber; i++) cout << "*"; 
    cout << endl;
}

void mine(const int readInEnd, const int maxCharBound)
{
    for (int readInStart = 1; readInStart < readInEnd+1; readInStart++) {
        bool goodInput = true;
        do {
            goodInput = true;
            try {
                showPrompt(readInEnd, maxCharBound);
                int readInNumber = getInput(maxCharBound);
                cout << "accepted number: " << readInNumber << endl;
                showOutput(readInNumber);
            } catch(int badNumber) {
                cout << "illegal number: " << badNumber << endl;
                goodInput = false;
            }
        } while (!goodInput);
    }
}

int main() { mine(5,81); return 0; } // forgot the return above :-(

Your refactoring





Format Copy from initial code

or Cancel