Help with Programming

Pages: 12
Write your question here.

1
2
  Brand new programmer in a class and could use all the help I can get.  Working on an assignment of a program outputting a grade average.  
How do I write this program and make it get the data from a file? Any help would be greatly appreciated. 
This is where class notes and example programs come in.

Look back to see how to open a file and read a number from it.

Make it a loop that ends when there are no more numbers. (You should have an example of this somewhere!)

Then, every time you read each grade, keep track of
  • the current sum of all grades read so far
  • the current number of grades read so far

When you are done, finish computing the average and cout it for the user.
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
//input variables
float NumberRight;
float TotalPoints;
float GradePercent;

ifstream inputFile;
inputFile.open("grades.txt");


// open file
inputFile.open("Grade.txt")

// read values
inputFile >> NumberRight >> TotalPoints;

//calculate values

GradePercent = NumberRight / TotalPoints

cout << "You got a "

if (GradePercent > 90)
{
cout "Excellent" endl;
}

if (GradePercent > 80)
{
cout "Well Done" endl;
}

if (GradePercent > 70)
{
cout "Good" endl;
}
else if (GradePercent >= 60)
{
cout "Need Improvement" endl;
}
else (GradePercent < 50)
{
cout "Fail" endl;
}

return 0;
}

I figured the easiest way to grab information from the file, Now Im stuck on the next step.
How do I use ceil, and are my grades cout correct? Or am I all jacked up @duthomhas
So are all the missing ; and << a deliberate thing you need to fix, or just a lack of attention to detail?

Because this might fall under the tutor assigned "fix the errors in this code" homework.

Choose one.
> inputFile.open("grades.txt");
> inputFile.open("Grade.txt")

definitely lack of attention to detail. I am not good at programming at all. Thank you for catching the grades error. Any suggestions on the rest? @salem
Yeah, start small, compile often, test everything that compiles.

If you can't write more than 5 lines at a time without getting lots of errors, there is no point writing 50 lines at once.

Eg.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    //input variables
    float NumberRight;
    float TotalPoints;
    float GradePercent;

    // open file
    inputFile.open("Grade.txt")

    // read values
    inputFile >> NumberRight >> TotalPoints;
    cout << "Read " << NumberRight << "," << TotalPoints << endl;
}

Make sure it compiles.
Make sure it runs, and produces the output you expect.

Then you add the next bit.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    //input variables
    float NumberRight;
    float TotalPoints;
    float GradePercent;

    // open file
    inputFile.open("Grade.txt")

    // read values
    inputFile >> NumberRight >> TotalPoints;
    cout << "Read " << NumberRight << "," << TotalPoints << endl;

    //calculate values
    GradePercent = NumberRight / TotalPoints;
    cout << "Grade " << GradePercent << endl;
}

Make sure it compiles.
Make sure it runs, and produces the output you expect.

If it doesn't compile, or doesn't produce the answer you expect, then you need to FIX what you have currently before adding more code.

Its giving me an error at
inputFile >> NumberRight>>TotalPoints;
says expects ';' before inputFile
@salem
> says expects ';' before inputFile
Of course it does.

Because it contains the error from your original code - just to see if you're paying attention.

READ the error message.
Study carefully the meaning of expects ';' and before.

It will have given you a line number to look at as well.

It's an absolutely essential skill to be able to work your way through error messages and match those to mistakes in your code.
Example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    //input variables
    float NumberRight;
    float TotalPoints;
    float GradePercent;

    // open file
    inputFile.open("Grade.txt"); //!! This is the ; that was expected "before"

    //!! This is the inputFile that the ; should be "before"
    // read values
    inputFile >> NumberRight >> TotalPoints;
    cout << "Read " << NumberRight << "," << TotalPoints << endl;
}
Thank you Salem that was a big help and got me going on the right track. All errors are gone and program is running but Im not getting an answer?

//input variables
float NumberRight;
float TotalPoints;
float GradePercent;

ifstream inputFile;
inputFile.open("grades.txt");

// open file
inputFile.open("grades.txt");

// read values
inputFile >> NumberRight >> TotalPoints;
cout << "Read" << NumberRight << "," << TotalPoints << endl;

if (TotalPoints >= 90)
cout << "Excellent." << endl;
else if (TotalPoints < 90 >= 80)
cout << "Well done." << endl;
else if (TotalPoints < 80 >= 70)
cout << "Good." << endl;
else if (TotalPoints < 70 >= 60)
cout << "Needs Improvement." << endl;
else if (TotalPoints < 60)
cout << "Fail." << endl;

return 0;
}

'Student's Grade.exe' (Win32): Loaded 'C:\Windows\SysWOW64\bcryptprimitives.dll'. Cannot find or open the PDB file.
'Student's Grade.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sechost.dll'. Cannot find or open the PDB file.

Does this mean my txt file is not being found?
Remember what I said about small changes, compile and test often.

You ran headlong for the finish line, and slammed straight into the first hurdle.

> Does this mean my txt file is not being found?
What do you think?
More specifically, what name is the text file?
I mean, you try to open TWO files with similar names.

> 'Student's Grade.exe' (Win32): Loaded 'C:\Windows\SysWOW64\bcryptprimitives.dll'. Cannot find or open the PDB file.
> 'Student's Grade.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sechost.dll'. Cannot find or open the PDB file.
You can ignore all the messages about PDB files.
The PDB (debugging symbol) errors have nothing to do with the logic of your code, you can ignore them, as salem c said.*

If you want to make sure your text file is opening, you can do
1
2
3
4
5
inputFile.open("grades.txt");
if (!inputFile)
{
    cout << "Error: Cannot open grades.txt\n";
}


What do you mean that you're "not getting an answer"? Is your program's line cout << "Read" << NumberRight << "," << TotalPoints << endl; not being printed?

*But if you want such PDB warnings to go away see: https://developercommunity.visualstudio.com/content/problem/25666/multiple-warning-messages-cannot-find-or-open-pdb.html
If you want the symbols for windows dlls loaded, you can enable "Microsoft Symbol Servers" in Debug->Options->Symbol Settings page. If you don't want to see symbol load warnings, you can turn off module load messages in the output window (right click on the output window to bring up the context menu to toggle these options). Hope this helps. Thanks!
Last edited on
I think it means it is not being found. I put the text file in resources under my project so it would be easy to find. The file name is "Grades.txt" I just have one file with 88, 90 on there for two grades thats all the teach asked for. How do I fix that?
@lilwhooday,

If you're not getting output, let me ask what IDE you're using. Some will flash the output and clear the window so fast you see nothing. Please a breakpoint on the last "return 0" of main, to stop the execution at that point, to see if that keeps the window up. Sometimes it isn't in the foreground, so look to see if your window is "behind" the editor. You should at least see something.

I've come to recognize people posting questions may be anywhere from 10 or 12 to 60 years of age.

Without giving too much information, could you hint to us if you're in high school or earlier, or beyond college age?

From here, all beginners seem about the same. A 30 something learning C++ may even be slower learning this than a 12 year old, but I do think it would inform the responders how patient or impatient we should be in discussion with someone on either side of the age of majority.

Compilers tend to flame throw error messages, and we all just get used to the fact it isn't personal. Sometimes experienced developers tend to respond like the compiler instead of teachers.

Edit:

About PDB files, this is the "program database" file, containing debug information. It is generated for the code you write, but there's one for every library loaded or used. You can expect the debugger will mention they can't be found for parts of the operating system and some of the C++ library code - and you usually don't need it. Ganado and Salem had that right.

Last edited on
Write this test code...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <fstream>

using namespace std;

int main() {
	ofstream myfile;
	myfile.open("example.txt", ios::out);
	myfile << "Writing this to a file.\n";
	myfile.close();


	return 0;
}


It should create a txt file and place it where it needs to be in order to access it. Then you will know exactly where to store your grades.txt file.
Last edited on
@Niccolo Im not sure what IDE is and what is breakpoint.
I am a 37 year old in college taking C++ for the first time and it is like learning a different language, I really don't understand it.
The black screen is flashing but it goes away immediately. Im trying to get the program to give me a grad and read "Excellent" but I don't see that.

@Manga I will try your code and see if that makes a difference.

The black screen is flashing but it goes away immediately.


THAT is exactly the symptom I'm expecting. The program may be displaying your text, but it disappears immediately. This is because of how you are launching your program.

You're probably double clicking the executable? If so, that opens a "command line window", which closes immediately upon exit of the program. Instead, you'll need to either run in a debugger (I'll get to that), or you must open a command line window (a terminal in Linux/Unix/MAC), and run your application at the command line.

Now, IDE means "Integrated Development Environment". This could be Eclipse, IntelliJ, Visual Studio CE (If you're in Windows). This provides all all encompassing set of tools and a text editor for development, and it is likely you should use one to simplify things.

What operating system are you using? This guides which IDE is best to use (many are free).

A debugger, usually used within an IDE, is a way of watching the program execute, and examining it "from the inside out". It is "xray vision" for programming. A breakpoint is a "mark" you make in your code where you tell the debugger to pause your program at that point, so you can see what is happening. You can step through your code one line at a time while it is executing. There's hardly a better way to visualize and understand what you're writing. You need one.

Are you following a particular text in school? Don't take this the wrong way, but I have my own opinions and reservations about what is taught in schools, and how it is taught. So I have this advice.

Get a copy of Stroustrup's "C++: Principles and Practice". It is intended for you. Stroustrup uses it as a text to teach beginners (through intermediate), but you don't need a classroom to learn from it.

It will do all that your school might not. It will not tell you how to use and install an IDE (that is a separate goal), but with a suitable IDE aimed at C++, that book is a doctor's prescription for a 30 something walking your path (I'm a 50 something, been at this for decades...and trust me, they've released new versions of this language in 98/03/11/14/17 and now again for 20 - and I'm going back to school (personally) every time).

That book will help you excel in the class, make sense of what the teacher might not explain well (or know).

No matter what school teaches this stuff, the only way any of us ever actually become good programmers is self study. It has been so for decades. Even if you are a master at some point, it moves forward and we all must restudy. It gets faster and easier - there's just a "critical mass" you are still developing in your mind.

Be patient about this particular level of confusion. It fades with practice, just keep at it. It will not be a foreign language for long.
Last edited on
The black screen is flashing but it goes away immediately.


Sounds like what's happening here is that your program is opened in a window, and then the program runs and writes some words to that window, and then the program finishes running, and now the program is finished, so the window closes. Logical, no? Makes perfect sense. Why would the window stay open? The program has finished.

Except, of course, that even AFTER the program has finished, you want to see what it wrote to that window.

We have a whole huge thread about how to do that here somewhere, but for the moment, you could just add this to the end of your program:

1
2
 cout << "Press ENTER to continue...";
 cin.ignore(100, '\n');


and then the program won't finish until you press ENTER, so you can examine the window at your leisure.


@Niccolo I am using Windows Microsoft Visual Studio. You guys are teaching me great steps. @Repeater the code you gave me got the screen to stay open and it gave me a crazy number. I think I forgot he ceil function.

This is the assignment Im working on
Write a C++ program that computes a student’s grade for an assignment as a percentage given the student’s score and total points. The final score must be rounded up to the nearest whole value using the ceil function in the <cmath> header file. You must also display the floating-point result up to 5 decimal places. The input to the program must come from a file containing a single line with the score and total separated by a space.

In addition, you must print to the console “Excellent” if the grade is greater than or equal to 90, “Well Done” if the grade is less than 90 and greater than or equal to 80, “Good” if the grade is less than 80 and greater than or equal to 70, “Need Improvement” if the grade is less than 70 and greater than or equal to 60, and “Fail” if the grade is less than 60.

Am I missing a lot?
Personally I find Visual Studio to be one of the best IDE's to use. While I get the anti-MS viewpoint, and some just prefer IntelliJ or even XCode on the MAC, VS CE is free and fantastic.

Depending on your keyboard settings you can set breakpoints by placing the cursor on a line and hitting F9 (check the debug menu and look for "toggle breakpoint".

Check the debug menu for "step over" and "step into". The "step into" means to trace the instruction into a function call, while "step over" means make the function call but stop on the next line in the current scope (current function). This is usually F10 (for step over) and F11 (for step into) - which only appear in the debug menu WHILE YOU'RE DEBUGGING. The debug menu has options to start debugging (set some breakpoint first).

Search for some kind of tutorial/walk through on using VS debugger. You can "watch" values, see the "call stack" and change values while debugging. Look at the debug menu for "windows" to see all the views you can open.

Do you have multiple monitors? It is a big help but not required. Many use a laptop.

@Repeater has a great point about causing a pause in the output, but if you set a breakpoint it does something similar.

What version of VS? Is it 2019? The got wise to the issue at some point and built in an automatic pause, but not in 2017 and before.

VS 2017 is ok, don't upgrade just because....older...mmmm...maybe reconsider.


It is a good idea to use the debugger just to walk through code to see how it works. Make sure you open a watch window and learn to use it. Autos and locals are good windows, too. Open a call stack window to see what happens as you trace into functions.

If you get curious you can open a disassembly window to see what the machine does to execute your code (but don't dwell on it for long).

Open a breakpoint window to see all the breakpoints you've set, and from there you can enable/disable/delete them, or set conditions on them (break when a value goes above a certain value for example - or break when some code changes a variable).
Last edited on
Pages: 12