Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Stuck in condition

Posted on 2011-02-14
11
Medium Priority
?
833 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
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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

610 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