Help me to complete this program.

Hi Guys,

I am writing a program as below:

1. Program starts.
2. The program will wait for user input and prompt user to input an integer value in between 2 to 10.
3. If the input integer in between 2 - 10 the program continues and show its multiplication value up to 10. if the input value is not in between 2 to 10 the program will display an error message " Number is out of range " and prompt user to input the number again.
4. Prompt the user to choose to continue further or not. if the user want to continue the program will continue, if not the program will exit.

I did some work around with this but doesn’t meet the code with the specification exactly.

Can anyone help me to develop this further and multiple solutions if there any.

My sample code goes here:

#include <iostream>

using namespace std;

int main()
{
int counter; int number; //declare variable
char nSelection;
cout << "\t" << "********** This is a multiplication testing program ***************" << endl;
cout << "Please make your choice, If you would like to continue Press y if not press n" << endl;
cin >> nSelection;

do{
if (nSelection=='y')
{
cout << "Please enter a number between 2 to 10" << endl;
cin >> number;

if ( number >=2 && number <=10)
{
for (counter=1; counter<=10; counter++)
cout << number << " X " << counter << " = " << number* counter<< endl;
}
else
{
cout << "Number is out of range !!!";
}
} else
{
cout << "The program is now terminating";
break;
}
} while(nSelection!='n');

return 0;
}
Well you have the "make your choice to continue" and your input of getting the actual choice outside of the do/while loop also YOu really need to learn proper indentation.
http://indentcode.net/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
 
using namespace std;
 
int main()
{
    int counter;
    int number;			//declare variable
    char nSelection;
    cout << "t" << "********** This is a multiplication testing program ***************" << endl;
    cout << "Please make your choice, If you would like to continue Press y if not press n" << endl;
    cin >> nSelection;
 
    do {
	if (nSelection == 'y') {
	    cout << "Please enter a number between 2 to 10" << endl;
	    cin >> number;
 
	    if (number >= 2 && number <= 10) {
		for (counter = 1; counter <= 10; counter++)
		    cout << number << " X " << counter << " = " << number * counter << endl;
	    } else {
		cout << "Number is out of range !!!";
	    }
	} else {
	    cout << "The program is now terminating";
	    break;
	}
    } while (nSelection != 'n');
 
    return 0;
}


Isn't that much easier to see where all your braces are?
Also might I suggest < or > instead of <= or >= it looks better imo.
Thanks.
#include<iostream> using namespace std;
int main()
{
int number, counter, mulval;
char yesno;
start:
cout<<''Enter a number(2 to 10):'' <<endl;
cin>>number;
if(number>=2 && number<=10)
{
for(counter=0; counter<=10; counter++)
{
mulval=number*counter;
cout<<number<<''X''<<counter<<''=''<<mulval<<endl;
}
}
else{ cout<<''Out of Range''<<endl;
goto start; }
cout<<''Want to continue? press(y/n)'' <<endl;
cin>>yesno;
if(yesno=='y' || yesno=='Y')
goto start;
return(0);
}
Last edited on
I don't mean to be harsh or anything...but...that's just...

1) don't use using namespace std unless it's a very big project and you're actually using majority of the stuff in it...
2) don't use goto instead use a do/while loop
1
2
3
4
do
{
    //stuff
} while( yesno == 'y' || yesno == 'Y' );

2b) Also yesno is kind of a bad name for a variable maybe something like
answer , response , continue_char , proceed_char or I supposed y_or_n_option or something along those lines. yesno is kind of vague and anyways that would imply it is a string not a character input.
3 ) L9 would look better as if( number > 1 && number < 11 )
4) L10 you have the loop running 11 times instead of 10 and he wants to multiply by 1 , 2 , 3 ... 10 so it should be either for( counter = 1 ; counter < 11; ++counter ) or for( counter = 10; counter > 0 ; --counter ) or for( counter = 0; counter < 10; ++counter )/*Then when you multiply multiply by counter + 1.*/ Those are the best options IMO
5) lastly but the most important proper indentation. I tried to tell you about this the other day =/
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    //4 spaces
    loop()
    {
        //4 spaces
        if()
        {
            //4 spaces
        }
    }
}
*4 spaces equals a tab. Most c++ text editors willl automatically indent but a lot of newer programmers like to remove them I have no idea why..it looks completely sloppy and you can not tell where your braces end.
giblit wrote:
don't use using namespace std unless it's a very big project and you're actually using majority of the stuff in it...
Iwould have suggested the opposite, that its fine only in very small (test) projects.

Most c++ text editors willl automatically indent but a lot of newer programmers like to remove them I have no idea why
Indentation is lost when code is posted without beeing wrapped in code tags, and even then it is often borked.
Topic archived. No new replies allowed.