Array or String Comparing Assignment

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2005
Posts: 6
Reputation: klmbrt is an unknown quantity at this point 
Solved Threads: 0
klmbrt klmbrt is offline Offline
Newbie Poster

Array or String Comparing Assignment

 
1
  #1
Feb 27th, 2005
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...
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 199
Reputation: Tight_Coder_Ex is an unknown quantity at this point 
Solved Threads: 14
Tight_Coder_Ex's Avatar
Tight_Coder_Ex Tight_Coder_Ex is offline Offline
Junior Poster

Re: Array or String Comparing Assignment

 
0
  #2
Feb 27th, 2005
By sounds of things you've sort of headed in the right direction with strcmp, and your ideas may not be as bad as you think. Give us an example of what you've come up with so far and take if from there.

Remember to enclose code using code tags.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 6
Reputation: klmbrt is an unknown quantity at this point 
Solved Threads: 0
klmbrt klmbrt is offline Offline
Newbie Poster

Re: Array or String Comparing Assignment

 
0
  #3
Feb 27th, 2005
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;
}
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 6
Reputation: klmbrt is an unknown quantity at this point 
Solved Threads: 0
klmbrt klmbrt is offline Offline
Newbie Poster

Re: Array or String Comparing Assignment

 
0
  #4
Feb 27th, 2005
An example of the input file would be:

ABC54301 TFTFTFTT TFTFTFFTTFT
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 6
Reputation: klmbrt is an unknown quantity at this point 
Solved Threads: 0
klmbrt klmbrt is offline Offline
Newbie Poster

Re: Array or String Comparing Assignment

 
0
  #5
Feb 27th, 2005
An example of the input file would be:

ABC54301 TFTFTFTT TFTFTFFTTFT

notice that one question would be unanswered in this example and the input starts with the Student ID and seperated only by a space.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 199
Reputation: Tight_Coder_Ex is an unknown quantity at this point 
Solved Threads: 14
Tight_Coder_Ex's Avatar
Tight_Coder_Ex Tight_Coder_Ex is offline Offline
Junior Poster

Re: Array or String Comparing Assignment

 
0
  #6
Feb 27th, 2005
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.


  1. char *Answer = &str [9];
  2. int Pos = 0, Correct = 0;
  3.  
  4. str [8] = 0; // Terminator at end of Students ID
  5.  
  6. while (Answer [Pos]) { // This will look after terminating at end of string
  7. if (Answer [Pos] == ' ') continue; // Ignore blanks in input
  8. if (Answer [Pos] == a[Pos]) Correct++; // Increment on match
  9. Pos++;
  10. }
  11.  
  12. single Ratio = (single (Answer) / single (Pos)) * 100
  13. 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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 6
Reputation: klmbrt is an unknown quantity at this point 
Solved Threads: 0
klmbrt klmbrt is offline Offline
Newbie Poster

Re: Array or String Comparing Assignment

 
0
  #7
Feb 27th, 2005
I'm not sure I understand the line:
single Ratio = (single (Answer) / single (Pos)) * 100
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 199
Reputation: Tight_Coder_Ex is an unknown quantity at this point 
Solved Threads: 14
Tight_Coder_Ex's Avatar
Tight_Coder_Ex Tight_Coder_Ex is offline Offline
Junior Poster

Re: Array or String Comparing Assignment

 
0
  #8
Feb 28th, 2005
Originally Posted by klmbrt
I'm not sure I understand the line:
single Ratio = (single (Answer) / single (Pos)) * 100
The method here is referred to as typecasting. In this case if you were to divide 19 by 21 lets say and leave them as integers you would get 0. Actually as integers anything other than 21 would yeild 0. You must coherse them to singles first and then the result will be .9047 multiplied by 100 you get 90.47%. I could have declared Answer & Pos as singles to begin with, but this causes unnecessary overhead for code as singles are not the natural data type for X86 CPU's
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 6
Reputation: klmbrt is an unknown quantity at this point 
Solved Threads: 0
klmbrt klmbrt is offline Offline
Newbie Poster

Re: Array or String Comparing Assignment

 
0
  #9
Feb 28th, 2005
So, what would I declare Single and Ratio as?
char single
char ration
maybe...
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 199
Reputation: Tight_Coder_Ex is an unknown quantity at this point 
Solved Threads: 14
Tight_Coder_Ex's Avatar
Tight_Coder_Ex Tight_Coder_Ex is offline Offline
Junior Poster

Re: Array or String Comparing Assignment

 
0
  #10
Feb 28th, 2005
single, double, float, int, short, long are all type specifiers so in the example

  1. single Ratio =

The specifier single tells the complier Ratio is a single precision value. You may be accustomed to seeing
  1. single Ratio;
  2.  
  3. Ratio = (single (Answer) / single (Pos)) * 100;

Most often within a statement block I'll combine the declration with statement
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC