#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0

    Need Serious C++ Help!


    Ok, so this is my first semester taking a C++ Programming course and I can truly say that it isn't easy for me. I'm currently working on the fifth program out of a total of six and my grade is very low. As a result, I'm desperately trying to get all the help I can get right now. The fifth program asks me to open up a file that contains names and test answers (e.g T for true or F for false). The answer key is included in the file as well. The program also asks me to check each student's answers comparing them to the answer key and then calculating their score and averaging all the scores at the bottom of a table I have to make. I'm very confused as to what I should do next; I'm not sure if I have correctly opened the file because when I execute the program, neither the names nor the answers show up. Please someone help me. Thank you!

    #include <iostream>
    #include <fstream>
    #include
    <string>
    using namespace std;

    int main()
    {
    const int ANSWER_SIZE = 30; // Answer key array size
    int answers[ANSWER_SIZE]; // Array with 30 elements
    int count = 0;
    ifstream xfile; // Input file stream object
    string name[30];
    const int STUDENT_ANSWERS = 30;
    char student[STUDENT_ANSWERS];



    // Open the file.
    xfile.open("xfile.txt");
    while (count < ANSWER_SIZE && xfile >> answers[count])
    count++;

    xfile.clear();
    xfile.close();
    cout << "The student names and answers are: ";
    for (count = 0; count < ANSWER_SIZE; count++)
    cout << answers[count] << " ";
    cout << endl;
    return 0;
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,089
    Rep Power
    2222
    Originally Posted by proudboy1026
    ... ; I'm not sure if I have correctly opened the file ...
    Then check the file object's status and find out! Knowing is much more superior to guessing.

    I never did like iostreams, so I don't use it myself. If you were using C file I/O, then the call to fopen would return a FILE pointer; if the open operation failed, then fopen would return a value of NULL. So then in C, you must always test the return value of fopen in order to tell whether you had successfully opened that file.

    Does the open method of the ifstream class return anything? Does that return value tell you whether the open operation succeeded? Don't you know that? Then why haven't you read the documentation for ifstream and for its open method? Knowing is far superior to guessing. RTFM! ("Read The Manual!")

    Does ifstream have other methods or properties that would tell you whether the file was open? I think I've seen something about a fail in passing. Shouldn't you read the documentation to find out?


    And please do us all a favor and stop substituting stupid colors for code tags -- keyword for code tags is [b]code[/i]. You need code tags to make your code readable by preserving its indentation. All those colors do is make an unreadable mess even more unreadable, especially that fracking stupid "MediumTurquoise" coloration that blends into the gray background.

    Here is what your code looks like when rendered readable (original indenting retrieved via Reply button; fracking stupid colors scrubbed out by hand):
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	const int ANSWER_SIZE = 30; // Answer key array size
    	int answers[ANSWER_SIZE];   // Array with 30 elements
    	int count = 0;
    	ifstream xfile;    // Input file stream object
    	string name[30];
    	const int STUDENT_ANSWERS = 30;
    	char student[STUDENT_ANSWERS];
    
    
    
    	// Open the file.
    	xfile.open("xfile.txt");
    	 while  (count < ANSWER_SIZE && xfile >> answers[count])
    		 count++;
    
    	xfile.clear();
    	xfile.close();
    	cout << "The student names and answers are: ";
    	for (count = 0; count < ANSWER_SIZE; count++)
    		cout << answers[count] << "  ";
    	cout << endl;
    	return 0;
    }
    Looks a lot better now.

    So then test whether that file actually opened.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by dwise1_aol
    Then check the file object's status and find out! Knowing is much more superior to guessing.

    I never did like iostreams, so I don't use it myself. If you were using C file I/O, then the call to fopen would return a FILE pointer; if the open operation failed, then fopen would return a value of NULL. So then in C, you must always test the return value of fopen in order to tell whether you had successfully opened that file.

    Does the open method of the ifstream class return anything? Does that return value tell you whether the open operation succeeded? Don't you know that? Then why haven't you read the documentation for ifstream and for its open method? Knowing is far superior to guessing. RTFM! ("Read The Manual!")

    Does ifstream have other methods or properties that would tell you whether the file was open? I think I've seen something about a fail in passing. Shouldn't you read the documentation to find out?


    And please do us all a favor and stop substituting stupid colors for code tags -- keyword for code tags is [b]code[/i]. You need code tags to make your code readable by preserving its indentation. All those colors do is make an unreadable mess even more unreadable, especially that fracking stupid "MediumTurquoise" coloration that blends into the gray background.

    Here is what your code looks like when rendered readable (original indenting retrieved via Reply button; fracking stupid colors scrubbed out by hand):
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	const int ANSWER_SIZE = 30; // Answer key array size
    	int answers[ANSWER_SIZE];   // Array with 30 elements
    	int count = 0;
    	ifstream xfile;    // Input file stream object
    	string name[30];
    	const int STUDENT_ANSWERS = 30;
    	char student[STUDENT_ANSWERS];
    
    
    
    	// Open the file.
    	xfile.open("xfile.txt");
    	 while  (count < ANSWER_SIZE && xfile >> answers[count])
    		 count++;
    
    	xfile.clear();
    	xfile.close();
    	cout << "The student names and answers are: ";
    	for (count = 0; count < ANSWER_SIZE; count++)
    		cout << answers[count] << "  ";
    	cout << endl;
    	return 0;
    }
    Looks a lot better now.

    So then test whether that file actually opened.

    Yes, the open method returned the same value 30 times; I know that much. I guess I just have to study the book some more and ask for help when I really need it.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,089
    Rep Power
    2222
    Originally Posted by proudboy1026
    Yes, the open method returned the same value 30 times; I know that much.
    But you're doing absolutely nothing with open's return value except to throw it away.

    Now you're claiming to know what that value was. But you never thought to test it?

    Originally Posted by proudboy1026
    I guess I just have to study the book some more and ask for help when I really need it.
    I did help you. I told you to do what had never occurred to you, to test whether the file had opened. I even informed you of two options: 1) the return value of open, or 2) other ifstream methods or properties that inform you of the file's status.

    Again, RTFM! Your text book is not authoritative, but the documentation is! What compiler or IDE are you using? Does it come with documentation for the standard library functions and classes? If so, have you bothered to read that yet? Go there before you go to your textbook.

    Your compiler/IDE didn't come with documentation? Then look it up on-line: STFW! ("Search The Web!")
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by dwise1_aol
    But you're doing absolutely nothing with open's return value except to throw it away.

    Now you're claiming to know what that value was. But you never thought to test it?


    I did help you. I told you to do what had never occurred to you, to test whether the file had opened. I even informed you of two options: 1) the return value of open, or 2) other ifstream methods or properties that inform you of the file's status.

    Again, RTFM! Your text book is not authoritative, but the documentation is! What compiler or IDE are you using? Does it come with documentation for the standard library functions and classes? If so, have you bothered to read that yet? Go there before you go to your textbook.

    Your compiler/IDE didn't come with documentation? Then look it up on-line: STFW! ("Search The Web!")
    Sorry for my ignorance, but how do you test the return value? I'm using Visual Basic Studio by the way and it does come with documentation for standard library functions and classes.
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,089
    Rep Power
    2222
    Originally Posted by proudboy1026
    Sorry for my ignorance, but how do you test the return value?
    With an if-statement. Either "if the return value indicates failure, then display error message and exit" or else "if the return value indicates failure, then display error message, else use the file." However you do it, you must not try to use the file unless it had opened successfully.

    Be forewarned. If your next question is "How do you get the return value", then I would have to ask you about your claim to have read that return value thirty times. BTW, since you say that its return value was the same every time, what exactly was that return value?

    Originally Posted by proudboy1026
    I'm using Visual Basic Studio by the way and it does come with documentation for standard library functions and classes.
    "Visual Basic Studio"? Whatever are you talking about? There is no such product that I am aware of.

    There is Visual Basic 20xx Express Edition, which you can download and use for free. But that only does Visual Basic, not C++.

    There is Visual C++ 20xx Express Edition, which you can download and use for free. But that only does C++, which is what you are trying to do. But why would you have called that "Visual Basic Studio"?

    There is Visual Studio 20xx (depending on the version), which you have to buy and pay for and which handles multiple languages.

    PS
    Now that I'm at work, I opened Visual Studio 2008 and consulted the help function for information on ifstream. The open method is defined as void, which means that it has no return value. So how could you have read a non-existent return value thirty times? And why would you have made such a statement which is obviously false?

    I also see that ifstream has another method that does tell you whether the file is open and that returns a bool value.

    Like I said, I never use iostreams, never having found any use for them in the past 23 years, since after having experienced the power and conciseness of C I/O it felt like regressing to Turbo Pascal. In C, you test the return value of fopen(). And I did tell you that other methods existed in iostreams to tell you the same thing.

    You do have the documentation. Have you read it yet?
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by dwise1_aol
    With an if-statement. Either "if the return value indicates failure, then display error message and exit" or else "if the return value indicates failure, then display error message, else use the file." However you do it, you must not try to use the file unless it had opened successfully.

    Be forewarned. If your next question is "How do you get the return value", then I would have to ask you about your claim to have read that return value thirty times. BTW, since you say that its return value was the same every time, what exactly was that return value?


    "Visual Basic Studio"? Whatever are you talking about? There is no such product that I am aware of.

    There is Visual Basic 20xx Express Edition, which you can download and use for free. But that only does Visual Basic, not C++.

    There is Visual C++ 20xx Express Edition, which you can download and use for free. But that only does C++, which is what you are trying to do. But why would you have called that "Visual Basic Studio"?

    There is Visual Studio 20xx (depending on the version), which you have to buy and pay for and which handles multiple languages.

    PS
    Now that I'm at work, I opened Visual Studio 2008 and consulted the help function for information on ifstream. The open method is defined as void, which means that it has no return value. So how could you have read a non-existent return value thirty times? And why would you have made such a statement which is obviously false?

    I also see that ifstream has another method that does tell you whether the file is open and that returns a bool value.

    Like I said, I never use iostreams, never having found any use for them in the past 23 years, since after having experienced the power and conciseness of C I/O it felt like regressing to Turbo Pascal. In C, you test the return value of fopen(). And I did tell you that other methods existed in iostreams to tell you the same thing.

    You do have the documentation. Have you read it yet?
    I'm sorry, I couldn't think of the IDE at the time: It's called Microsoft Visual Studio Express 2012 for Windows Desktop. And as you may have already picked up a long time ago: I had no clue what I was talking about regarding the return value. And no, I haven't read the documentation for it yet. Also, I was finally able to successfully open the data file; I'm just trying to figure out a loop to use for it in order to display the 25 student names and their answer, which has 30 characters. Then I have to calculate their score based on the test answer key - which I'm not sure on how to do - and then calculate the mean test score for the entire class.
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,089
    Rep Power
    2222
    The reason why I'm emphasizing that you read the documentation is because you absolutely need to become intimately familiar with the help system in Visual Studio (and in any other development system you may use in the future). It's purely a matter of simple practicality. As you work on your program, you will encounter several syntax problems regarding library functions and classes. These problems will involve what arguments those functions and methods take, what their return types are, and what those return values mean. And for the classes they will involve what the class' members are and how to use them. Furthermore, you may need to learn what a class' base class is in order to find its members that the class in question had inherited.

    The simple practicality of this is one of turn-round time. When you have those questions, you need the answer to them now. If you have to post those questions on a forum and wait for somebody to give you the answer, then several minutes or even hours could pass before you'd get a response, which more often than not would be "RTFM!". When I was in school, the only place we had any access to the computer (an IBM mainframe) was down in the computer center. Until you moved up to upper-division classes, all you had to work with were keypunch machines and there was about an hour turn-round between when you'd submit your card deck and when you'd receive your print-out. Once you moved up you got a virtual system account which cut your turn-around time down to several minutes, but then you'd often have to wait for one of the 10 terminals to free up. Needless to say, there would be several of us in the computer center past midnight working on our assignments and projects. We couldn't afford to waste any time.

    "RTFM!" ("Read The Manual!") has a long history in the industry, one which reaches back into the ancient times of circa 1970, if not earlier. It was the response to programmers who wouldn't look that information up for themselves in the manual, AKA "the documentation". It wasn't because the people they were asking were lazy, but rather because looking up that kind of information for yourself is a basic programming skill. If everybody just gave you the answer, then you would never learn that basic and essential skill. And now with the Internet we have added "STFW!" ("Search The Web!") for people who basically ask us to do their Google searches for them; again, it's a basic skill.

    Now, there are valid questions to be asked. For example, how can you look up a word in the dictionary if you have no idea what that word is? So it's valid to ask what function you would use for a particular task, but then once it's been named you should be able to then look it up for the particulars. And even if you don't know what property or method of a particular class to use (eg, of ifstream), the documentation will list those methods and properties and so you could look for one that should do what you need. Such as basic ifstream's method for testing whether the file is open. And the more you use

    Originally Posted by proudboy
    And no, I haven't read the documentation for it yet. Also, I was finally able to successfully open the data file; I'm just trying to figure out ...
    Does that mean that you are giving up on testing whether your file is open? Bad move. You need to solve each problem before you can move on.

    Attempt to open the file. Then test whether the file is open. What do you want your program to do if it cannot open the file? I assume that would be to report the error and exit. So write the code to do that. Remember, you only want to try to do anything with the file if and only if it is open. Make that correction and show it to us. With code tags. It makes no sense to proceed without taking care of this -- for one thing, your code is small enough to make it easier at this point. You need clearly defined regions of your main function for when the file has opened successfully and for where it has failed to open.

    Originally Posted by proudboy
    ... I'm just trying to figure out a loop to use for it in order to display the 25 student names and their answer, which has 30 characters. Then I have to calculate their score based on the test answer key - which I'm not sure on how to do - and then calculate the mean test score for the entire class.
    OK, I see that you have arrays for student names and for student answers, both of which contain 30 elements. So then open the file, read the data from the file (you'd be using the file stream's << operator in accordance with the file's format), close the file, and then process the data you have read in. That's all you'd need file I/O for.

    Though looking at your declarations and at your problem description, I have a stupid question: How many answers are associated with each student? The way you have it set up now with the BASIC data association style (that's the BASIC language), it looks like each student only has one and only one answer. And I am assuming that the array, answers, is the answer key. So why would the answer key also be set up to be associated with individual students? That doesn't make any sense.

    Instead, is it your intention that each student has thirty answers associated with him? Then the answers array can be compared to each student's answers, so that the correct responses can be tallied up to arrive at a score for each student? Is that your intention?

    Two solutions:
    1. Declare student to be a two-dimensional array with the rows indexed by each student's index number, thus associating them BASIC-style with the corresponding student. You would also need to declare a scores array also associated with each student via the index number. Very BASIC, not very C++.

    or
    2. Declare a student struct whose fields would be the student's name, an array of the student's answers, and the student's score. Then declare an array of student structs. The rest should fall right into place. Very C, also very C++, though C++ can take it even further.

    In designing a program, the choice of data structures is very important and will dictate the code.

IMN logo majestic logo threadwatch logo seochat tools logo