3 Replies - 368 Views - Last Post: 16 October 2019 - 12:24 PM Rate Topic: -----

#1 PendeJoe   User is online

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 09-October 19

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,
any advice?


#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 << "Sorry. Your answer is incorrect. Please try again.\n";
            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 << "Sorry. Your answer is incorrect. Please try again.\n";
            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 << "Sorry. Your answer is incorrect. Please try again.\n";
            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   User is offline

  • Code herder
  • member icon

Reputation: 7109
  • View blog
  • Posts: 24,146
  • 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.

I highly suggest making your indentation more consistent. That will help you visualize the flow and patterns in your code. That will help you see the similarities of the duplicated code.

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.
Was This Post Helpful? 2
  • +
  • -

#3 PendeJoe   User is online

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 09-October 19

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.

Was This Post Helpful? 0
  • +
  • -

#4 TightCoderEx   User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • 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 {
		int Sum, Answer;
		X1 = rand () % 90 + 10;
		X2 = rand () % 90 + 10;
		Sum = X1 + X2;
		
		cout << "\n   Question " << 4 - count << ":\t" << X1 << " + " << X2 << "\t= ";

		do {
			cin >> Answer;
			
			if (Answer == Sum)
				break;
			if (Answer <= 0)
				return -16;
				
			cout << "\t\t Wrong\t= ";	
		} while (true);		
		
	} while (--count);
	
	return EXIT_SUCCESS;
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1