?
Solved

Stuck in condition

Posted on 2011-02-14
11
Medium Priority
?
828 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
11 Comments
 
LVL 12

Accepted Solution

by:
mwochnick earned 400 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
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
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 1600 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Suggested Courses

764 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