Avatar of mustish1
mustish1

asked on 

Stuck in condition

Hi guys: Can any one please help me to add the condition in the program if the score is greater than 100 points the program should ask the user if the score is correct. The program should not add a score that is more than 100 points to the accumulator without the user permission.  Thanks.

#include <iostream>
using namespace std;

int main()
{
      int score = 0;
      int totalPoints = 0;
      char grade = ' ';
      cout << "First score (-1 to stop):";
      cin >> score;
      while(score != -1)
      {
            totalPoints +=score;
            cout << "Next score (-1 to stop):";
            cin >> score;
      }
      if(totalPoints >=315)
            grade = 'A';
      else if(totalPoints >= 280)
            grade = 'B';
      else if(totalPoints >=245)
            grade = 'C';
      else if(totalPoints >=210)
            grade = 'D';
      else
            grade = 'F';
      cout << "Total Points earned:" << totalPoints << endl;
      cout << "Grade:" << grade << endl;
      system("pause");
      return 0;
}      
Programming Languages-OtherProgrammingAlgorithms

Avatar of undefined
Last Comment
mustish1
ASKER CERTIFIED SOLUTION
Avatar of mwochnick
mwochnick
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of himanshut
himanshut
Flag of Australia image

replace your while condition with this one, and add the char confirm and char yes in variable declaration.

It's been years since I did c++/c hoping there shouldnt be any syntax errors

Cheers!
char confirm = 'y';
char yes = 'y';
          while(score != -1)
          {
             cout << "Next score (-1 to stop):";
            cin >> score;
            if(score>=100)
            {
                 cout<<"Is the score correct?"<<endl;
                 cin>>confirm;
                 if(confirm == yes)
                 {
                        totalPoints +=score; 
                 }
                cout<<"score not added"<<endl;
           }
}

Open in new window

Avatar of mustish1
mustish1

ASKER

I tried but getting errors
#include <iostream>
using namespace std;

int main()
{
      int score = 0;
        char confirm = 'y';
        char yes = 'y';
      int totalPoints = 0;
      char grade = ' ';
      cout << "First score (-1 to stop):";
      cin >> score;
      while(score != -1)
      {
            cout << "Next score (-1 to stop):";
            cin >> score;
            if(score>=100)
            {
                 cout<<"Is the score correct?"<<endl;
                 cin>>confirm;
                 if(confirm == yes)
                  {
                  totalPoints +=score;
                  }
                      cout<<"score not added"<<endl;
            }
                  cout << "Next score (-1 to stop):";
            cin >> score;
      }
      if(totalPoints >=315)
            grade = 'A';
      else if(totalPoints >= 280)
            grade = 'B';
      else if(totalPoints >=245)
            grade = 'C';
      else if(totalPoints >=210)
            grade = 'D';
      else
            grade = 'F';
      cout << "Total Points earned:" << totalPoints << endl;
      cout << "Grade:" << grade << endl;
      system("pause");
      return 0;
}      
Avatar of himanshut
himanshut
Flag of Australia image

what errors you get?
Avatar of mustish1
mustish1

ASKER

1>------ Build started: Project: cp7, Configuration: Debug Win32 ------
1>  test.cpp
1>  cp7.cpp
1>  Generating Code...
1>  LINK : C:\Users\Kathleen\documents\visual studio 2010\Projects\cp7\Debug\cp7.exe not found or not built by the last incremental link; performing full link
1>test.obj : error LNK2005: _main already defined in cp7.obj
1>C:\Users\Kathleen\documents\visual studio 2010\Projects\cp7\Debug\cp7.exe : fatal error LNK1169: one or more multiply defined symbols found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Avatar of himanshut
himanshut
Flag of Australia image


while(score != -1)
      {
            cout << "Next score (-1 to stop):";
            cin >> score;
            if(score>=100)
            {
                 cout<<"Is the score correct?"<<endl;
                 cin>>confirm;
                 if(confirm == yes)
                  {
                  totalPoints +=score;
                  }
              }
              else
                {
                    cout<<"score not added"<<endl;

                }
                  cout << "Next score (-1 to stop):";
            cin >> score;
      }

Open in new window

Avatar of mustish1
mustish1

ASKER

There is no errors but it dont shows the grade

#include <iostream>
using namespace std;

int main()
{
      int score = 0;
      int totalPoints = 0;
      char grade = ' ';
      char confirm = ' ';
      char yes = 'y';
      cout << "First score (-1 to stop):";
      cin >> score;
while(score != -1)
      {
            cout << "Next score (-1 to stop):";
            cin >> score;
            if(score>=100)
            {
                 cout<<"Is the score correct?"<<endl;
                 cin>>confirm;
                 if(confirm == yes)
                  {
                  totalPoints +=score;
                  }
              }
              else
                {
                    cout<<"score not added"<<endl;

                }
                  cout << "Next score (-1 to stop):";
            cin >> score;
      }

      if(totalPoints >=315)
            grade = 'A';
      else if(totalPoints >= 280)
            grade = 'B';
      else if(totalPoints >=245)
            grade = 'C';
      else if(totalPoints >=210)
            grade = 'D';
      else
            grade = 'F';
      cout << "Total Points earned:" << totalPoints << endl;
      cout << "Grade:" << grade << endl;
      system("pause");
      return 0;
}      
SOLUTION
Avatar of himanshut
himanshut
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of mustish1
mustish1

ASKER

Thanks a lot.
Avatar of himanshut
himanshut
Flag of Australia image

Right,
I missed out some checks.

Now you can run the code provided.
TO confirm you need to enter only y
other than y it will not add up the score. Give it a go :)

// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"




#include <iostream>
using namespace std;

int main()
{
      int score = 0;
      char confirm = 'y';
      char yes = 'y'; 
      int totalPoints = 0;
      char grade = ' ';
      cout << "First score (-1 to stop):";
      cin >> score;
      while(score != -1)
      {

            if(score>=100)
            {
                 cout<<"Is the score correct?"<<endl;
                 cin>>confirm;
                 if(confirm == yes)
                  {
                  totalPoints +=score;
                  }
				 else
				 {
					 cout<<"score not added"<<endl;
				 }
                      
            }
			else
			{
				 totalPoints +=score;
			}
            cout << "Next score (-1 to stop):";
            cin >> score;
      }
      if(totalPoints >=315)
            grade = 'A';
      else if(totalPoints >= 280)
            grade = 'B';
      else if(totalPoints >=245)
            grade = 'C';
      else if(totalPoints >=210)
            grade = 'D';
      else
            grade = 'F';
      cout << "Total Points earned:" << totalPoints << endl;
      cout << "Grade:" << grade << endl;
      system("pause");
      return 0;
}

Open in new window

Avatar of mustish1
mustish1

ASKER

Thanks again.
Programming
Programming

Programming includes both the specifics of the language you’re using, like Visual Basic, .NET, Java and others, but also the best practices in user experience and interfaces and the management of projects, version control and development. Other programming topics are related to web and cloud development and system and hardware programming.

55K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo