# Feedback on my assignment

Posted 09 October 2019 - 07:56 PM

Hey, this is my first course in programming
and i wonder how i could simplify my code.

A buddy told me that i have repeated codes
and recommended me to do an outer while-loop
but i cant seem to figure out how to do that,

```#include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;

int main()
{
int X1, X2, sum, Xsum;

srand(time(0));

X1 = rand() % 90 + 10;
X2 = rand() % 90 + 10;

Xsum = X1 + X2;

cout << "Question 1: " << X1 << " + " << X2 << " = ";
cin >> sum;

if (sum == Xsum)
{
cout << "Correct answer! " << X1 << " + " << X2 << " = " << Xsum << endl;
cout << "Move to the next question.\n";
}
else
{
while(sum != Xsum)
{
cout << "Question 1: " << X1 << " + " << X2 << " = ";
cin >> sum;
if (sum == Xsum)
break;
}

cout << "Correct answer! " << X1 << " + " << X2 << " = " << Xsum << endl;
cout << "Move to the next question.\n";
}

X1 = rand() % 90 + 10;
X2 = rand() % 90 + 10;

Xsum = X1 + X2;

cout << "Question 2: " << X1 << " + " << X2 << " = ";
cin >> sum;

if (sum == Xsum)
{
cout << "Correct answer! " << X1 << " + " << X2 << " = " << Xsum << endl;
cout << "Move to the next question.\n";
}
else
{
while(sum != Xsum)
{
cout << "Question 2: " << X1 << " + " << X2 << " = ";
cin >> sum;
if (sum == Xsum)
break;
}

cout << "Correct answer! " << X1 << " + " << X2 << " = " << Xsum << endl;
cout << "Move to the next question.\n";
}

X1 = rand() % 90 + 10;
X2 = rand() % 90 + 10;

Xsum = X1 + X2;

cout << "Question 3: " << X1 << " + " << X2 << " = ";
cin >> sum;

if (sum == Xsum)
{
cout << "Correct answer! " << X1 << " + " << X2 << " = " << Xsum << endl;
}
else
{
while(sum != Xsum)
{
cout << "Question 3: " << X1 << " + " << X2 << " = ";
cin >> sum;

if (sum == Xsum)
{
cout << "Good job you completed all 3 questions!\n";
break;
}

}
}
return 0;
}

```

This post has been edited by Skydiver: 09 October 2019 - 08:13 PM
Reason for edit:: Put code in code tags. Learn to do this yourself.

Is This A Good Question/Topic? 0

## Replies To: Feedback on my assignment

### #2 Skydiver Reputation: 7140
• Posts: 24,250
• Joined: 05-May 12

## Re: Feedback on my assignment

Posted 09 October 2019 - 08:21 PM

Look at your lines 14-40. They look just like lines 42-68 and 70-97. The only variations are your text messages depending on whether this is the last question or if there will be more questions.

Your friend did you a dis-service by suggesting using an outer while loop. An outer for loop would better serve you because you know exactly how many iteration you want -- 3 iterations. You can use the iteration number to modify your text about what question number is being asked, as well as determine whether there will be another question of if this is the last question.

## Re: Feedback on my assignment

Posted 14 October 2019 - 03:44 PM

Hey, it was actually my first time using this forum so couldnt figure out how to see replys for some reason.
yeah i figured it out that it was better to use the for-loop. Appreciate the feedback!!

This post has been edited by Skydiver: 16 October 2019 - 12:59 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

### #4 TightCoderEx Reputation: 1
• Posts: 3
• Joined: 01-December 12

## Re: Feedback on my assignment

Posted 16 October 2019 - 12:24 PM

Functionally, except for a few formatting aspects, this code is the same as yours except there is one thing that handles a specific error that yours does not. Line 9 is peculiar to LINUX terminal, so if your using W\$ it probably won't work.

```#include	<iostream>
#include	<ctime>

using namespace std;

int main (int argc, char **argv) {
int X1, X2, count;

cout << "\033[2J\033[4;1H";			// Clear screen and position cursor in BASH
srand (time(0));
count = 3;

do {
X1 = rand () % 90 + 10;
X2 = rand () % 90 + 10;
Sum = X1 + X2;

cout << "\n   Question " << 4 - count << ":\t" << X1 << " + " << X2 << "\t= ";

do {

break;
return -16;

cout << "\t\t Wrong\t= ";
} while (true);

} while (--count);

return EXIT_SUCCESS;
}

```

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }