| | |
Array or String Comparing Assignment
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2005
Posts: 6
Reputation:
Solved Threads: 0
I am attempting to write a C++ program that has the results of a T/F test. It takes the information from a data file and then it compares the students answers to the correct answers. I have two problems. First the data file has the first 8 characters as the Student ID(Letters and Numbers), then it has a blank space followed by the student answers. Can someone give me some ideas about how I would maybe use strcmp or would I use a method of comparing Arrays?The output would be the student ID followed by the student answers and then a Grade.
I just need help getting started in the right direction. So far my ideas have been bad...
•
•
Join Date: Feb 2005
Posts: 6
Reputation:
Solved Threads: 0
This is a start to what I had in mind:
/* Preprocessor Directives: ********************************************************/
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
using namespace std;
/* Begin Main *********************************************************/
int main()
{
char a[21]={'T','F','F','T','F','F','T','T','T','T','F','F',
'T','F','T','F','T','F','T','\0'};
char str[30];
ifstream inFile;
inFile.open ("grades.dat.c_str");
if (!inFile)
{
cout << "Could not open file.\n";
return 1;
}
while (inFile)
{
cin.get(str[30]);
strcpy(str, "grades.dat.c_str");
if (strcmp (a[0], str[9])<0)
{
cout << strcmp;
}
cout << str[0,1,2,3,4,5,6,7] << endl;
}
inFile.close();
return 0;
}
/* Preprocessor Directives: ********************************************************/
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
using namespace std;
/* Begin Main *********************************************************/
int main()
{
char a[21]={'T','F','F','T','F','F','T','T','T','T','F','F',
'T','F','T','F','T','F','T','\0'};
char str[30];
ifstream inFile;
inFile.open ("grades.dat.c_str");
if (!inFile)
{
cout << "Could not open file.\n";
return 1;
}
while (inFile)
{
cin.get(str[30]);
strcpy(str, "grades.dat.c_str");
if (strcmp (a[0], str[9])<0)
{
cout << strcmp;
}
cout << str[0,1,2,3,4,5,6,7] << endl;
}
inFile.close();
return 0;
}
I gather it will be safe to assume students results will always start at position 10. This is just one example of how it can be done.
There are many variations of this same method and there may even be better ones. Simply, think about the application conceptually (how you would calcuate the result manually) and then code accordingly.
C++ Syntax (Toggle Plain Text)
char *Answer = &str [9]; int Pos = 0, Correct = 0; str [8] = 0; // Terminator at end of Students ID while (Answer [Pos]) { // This will look after terminating at end of string if (Answer [Pos] == ' ') continue; // Ignore blanks in input if (Answer [Pos] == a[Pos]) Correct++; // Increment on match Pos++; } single Ratio = (single (Answer) / single (Pos)) * 100 cout << str << " " << Answer << " " << Ratio << endl;
There are many variations of this same method and there may even be better ones. Simply, think about the application conceptually (how you would calcuate the result manually) and then code accordingly.
•
•
•
•
Originally Posted by klmbrt
I'm not sure I understand the line:
single Ratio = (single (Answer) / single (Pos)) * 100
single, double, float, int, short, long are all type specifiers so in the example
The specifier single tells the complier Ratio is a single precision value. You may be accustomed to seeing
Most often within a statement block I'll combine the declration with statement
C++ Syntax (Toggle Plain Text)
single Ratio =
The specifier single tells the complier Ratio is a single precision value. You may be accustomed to seeing
C++ Syntax (Toggle Plain Text)
single Ratio; Ratio = (single (Answer) / single (Pos)) * 100;
Most often within a statement block I'll combine the declration with statement
![]() |
Other Threads in the C++ Forum
- Previous Thread: Object-Oriented Programming
- Next Thread: Help me understand function in c++
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





