> C++ program wont run, help please!?

C++ program wont run, help please!?

Posted at: 2014-12-18 
You'll need to recreate the project. Follow these steps for any standard C++ console project you create with Visual Studio:

1. Start with a C++ Win32 Console Application. Give the project a name, maybe set the location, and click OK.

2. Do NOT click "Finish". Click "Application Settings" or "Next" to get the "Applications Settings" dialog.

3. Uncheck "Precompiled header" and "SDL", then check "Empty Project". Now you can click "Finish" to create the project.

4. Use Project>Add New Item... or Ctrl+Shift+A to add a C++ source file to your project. Call it "main.cpp" or "lab2.cpp" or whatever you like. I like to make the main file have the same name as the project, but that's not everybody's choice.

Now you can copy/paste the code from your first attempt.

The advantage to using a console project and marking it empty is that you can use Ctrl+F5 or Debug>Start Without Debugging to run your program and you'll get a pause at end-of-run to see the output, without a hack like system("pause"), and it works even if some called function uses exit() to end the program without returning to main().

Visual C++ is a good compiler with very fast compile times and good support for error detection in Debug builds. It's a pain to launch and create simple standard project, though, so I will usually fire up Code::Blocks and use the MinGW compiler for anything simple.

You might want to pick it up just to see some places where your code might not be portable. The (C::B + MinGW bundle for Windows is the easiest choice. Look in "binaries".)

http://www.codeblocks.org

You have two problems here. The one you asked about has to do with how you set up your project. You may need to include stdafx.h or else configure your project as an empty project (with no files).

The second problem is that you are referencing your array starting with an index of 1 when you should be starting with an index of 0. You are overshooting the bounds of the array when you do this: grades[5]. This messes up your average greatly.

int grades[5] { grade1, grade2, grade3, grade4, grade5 };

to

int grades[5] ={ grade1, grade2, grade3, grade4, grade5 };

at line no 18 or 19

#include

using namespace std;

void letterGrade(int x);

int main()

{

int grade1, grade2, grade3, grade4, grade5;

cout << "Please enter your five grades: " << endl;

cin >> grade1;

cin >> grade2;

cin >> grade3;

cin >> grade4;

cin >> grade5;

int sum = grade1 + grade2 + grade3 + grade4 + grade5;

int average = sum / 5;

cout << "Your average grade is: " << average << endl;

letterGrade(average);

cin.get();

return 0;

}

void letterGrade(int x)

{

if (x >= 90 && x <= 100)

cout << "Your grade is: A" << endl;

else if (x >= 80 && x <= 89)

cout << "Your grade is: B" << endl;

else if (x >= 70 && x <= 79)

cout << "Your grade is: C" << endl;

else if (x >= 60 && x <= 69)

cout << "Your grade is: D" << endl;

else if (x >= 0 && x <= 59)

cout << "Your grade is: F" << endl;

}

Thanks i've corrected that, but still i get the same error. Any more help will be appreciated. Thanks

My program doesn't work. Can someone please explain why?

my code:

#include

using namespace std;

void letterGrade(int x);

int main()

{

int grade1, grade2, grade3, grade4, grade5;

cout << "Please enter your five grades: " << endl;

cin >> grade1;

cin >> grade2;

cin >> grade3;

cin >> grade4;

cin >> grade5;

int grades[5] { grade1, grade2, grade3, grade4, grade5 };

int sum = grades[1] + grades[2] + grades[3] + grades[4] + grades[5];

int average = sum / 5;

cout << "Your average grade is: " << average << endl;

letterGrade(average);

cin.get();

return 0;

}

void letterGrade(int x)

{

if (x >= 90 && x <= 100)

cout << "Your grade is: A" << endl;

else if (x >= 80 && x <= 89)

cout << "Your grade is: B" << endl;

else if (x >= 70 && x <= 79)

cout << "Your grade is: C" << endl;

else if (x >= 60 && x <= 69)

cout << "Your grade is: D" << endl;

else if (x >= 0 && x <= 59)

cout << "Your grade is: F" << endl;

}

The error shown:

"Error 2 error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source? c:\users\ben\documents\c++\tutorial 10 and 11 (if statements)\tutorial 10 and 11 (if statements)\tutorial 10 and 11 (if statements).cpp 53 1 Tutorial 10 and 11 (If statements)"