Solved

Stuck in condition

Posted on 2011-02-14
11
813 Views
Last Modified: 2012-05-11
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;
}      
0
Comment
Question by:mustish1
  • 5
  • 5
11 Comments
 
LVL 12

Accepted Solution

by:
mwochnick earned 100 total points
ID: 34893860
you need to check the value of score before you add it to totalPoints the logic should be something like
if score greater than or equal to 100
   ask if they are sure
   if yes
     then continue
   else
     ask them to reenter
else
  continue as normal
0
 
LVL 10

Expert Comment

by:himanshut
ID: 34893872
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

0
 

Author Comment

by:mustish1
ID: 34893918
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;
}      
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:himanshut
ID: 34893923
what errors you get?
0
 

Author Comment

by:mustish1
ID: 34893924
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 ==========
0
 
LVL 10

Expert Comment

by:himanshut
ID: 34893931

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

0
 

Author Comment

by:mustish1
ID: 34893951
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;
}      
0
 
LVL 10

Assisted Solution

by:himanshut
himanshut earned 400 total points
ID: 34893954
I ran this in Visual Studion creating win32 console app and it worked fine without any errors.
Can you please try doing the same again
// 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)
      {
            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;
}

Open in new window

0
 

Author Closing Comment

by:mustish1
ID: 34893964
Thanks a lot.
0
 
LVL 10

Expert Comment

by:himanshut
ID: 34893967
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

0
 

Author Comment

by:mustish1
ID: 34893976
Thanks again.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question