Solved

Stuck in condition

Posted on 2011-02-14
11
793 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 10

Expert Comment

by:himanshut
Comment Utility
what errors you get?
0
 

Author Comment

by:mustish1
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 10

Expert Comment

by:himanshut
Comment Utility

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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks a lot.
0
 
LVL 10

Expert Comment

by:himanshut
Comment Utility
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
Comment Utility
Thanks again.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bunnyEars challenge 6 62
Python 2.7 - French characters 6 46
endX challenge 2 47
Non-recursive backtracking, using a stack 1 43
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now