The following code is not performing correctly. It opens the username file but does not display the mark.

#include <cstdlib>							//these commands are pre processors directive
#include <fstream>							//required for library function
#include <iostream>
#include <cstring>
#include <string>

using namespace std;						 
										

int main()
{
	cout << "Marking Program"<<endl;			

	string username;

	cout << "Enter Students Username:";		
	cin >> username;
	
	string filename = username + ".txt";
    ifstream file(filename.c_str());
	if (!file) {
    cerr<<"Username not found"<<endl;		
    return EXIT_FAILURE;					
	
		
  string string1 ("DDRA");
  string string2 ("DDRB"); 	
  string string3 ("0021");	
  string string4 ("CLR");
  string string5 ("LDAA");
  string string6 ("LDAB");
  string string7 ("READ");
  string string8 ("ABA");
  string string9 ("STAA");
  string string10 ("END");

	
  int counter;
  counter=0;
  if (string1.find(filename.c_str()))		      
 ++counter;
 
 if (string2.find(filename.c_str()))		      
 ++counter;
	
 if (string3.find(filename.c_str()))		      
 ++counter;

 if (string4.find(filename.c_str()))		      
 ++counter;
	
 if (string5.find(filename.c_str()))		      
 ++counter;
	
 if (string6.find(filename.c_str()))		      
 ++counter;
	
 if (string7.find(filename.c_str()))		      
 ++counter;

 if (string8.find(filename.c_str()))		      
 ++counter;

 if (string9.find(filename.c_str()))		      
 ++counter;

 if (string10.find(filename.c_str()))		      
 ++counter;

 cout<< "students mark is"<<counter<<"out of 10"<<endl;	
     return EXIT_SUCCESS;									
}
}

after the username is entered the message press any key appears but no mark is displayed

Recommended Answers

All 5 Replies

string string1 ("DDRA");
  string string2 ("DDRB"); 	
  string string3 ("0021");	
  string string4 ("CLR");
  string string5 ("LDAA");
  string string6 ("LDAB");
  string string7 ("READ");
  string string8 ("ABA");
  string string9 ("STAA");
  string string10 ("END");

This is a sign you should be using an array.

int counter;
  counter=0;
  if (string1.find(filename.c_str()))		      
 ++counter;
 
 if (string2.find(filename.c_str()))		      
 ++counter;
	
 if (string3.find(filename.c_str()))		      
 ++counter;

 if (string4.find(filename.c_str()))		      
 ++counter;
	
 if (string5.find(filename.c_str()))		      
 ++counter;
	
 if (string6.find(filename.c_str()))		      
 ++counter;
	
 if (string7.find(filename.c_str()))		      
 ++counter;

 if (string8.find(filename.c_str()))		      
 ++counter;

 if (string9.find(filename.c_str()))		      
 ++counter;

 if (string10.find(filename.c_str()))		      
 ++counter;

This is a sign you should be using a loop.

>after the username is entered the message press any key appears but no mark is displayed
Your braces are misplaced. Here's the code with proper indentation:

#include <cstdlib>	//these commands are pre processors directive
#include <fstream> //required for library function
#include <iostream>
#include <cstring>
#include <string>

using namespace std;						 


int main()
{
  cout << "Marking Program"<<endl;			

  string username;

  cout << "Enter Students Username:";		
  cin >> username;

  string filename = username + ".txt";
  ifstream file(filename.c_str());
  if (!file) {
    cerr<<"Username not found"<<endl;		
    return EXIT_FAILURE;					

    string string1 ("DDRA");
    string string2 ("DDRB"); 	
    string string3 ("0021");	
    string string4 ("CLR");
    string string5 ("LDAA");
    string string6 ("LDAB");
    string string7 ("READ");
    string string8 ("ABA");
    string string9 ("STAA");
    string string10 ("END");

    int counter;
    counter=0;
    if (string1.find(filename.c_str()))		      
      ++counter;

    if (string2.find(filename.c_str()))		      
      ++counter;

    if (string3.find(filename.c_str()))		      
      ++counter;

    if (string4.find(filename.c_str()))		      
      ++counter;

    if (string5.find(filename.c_str()))		      
      ++counter;

    if (string6.find(filename.c_str()))		      
      ++counter;

    if (string7.find(filename.c_str()))		      
      ++counter;

    if (string8.find(filename.c_str()))		      
      ++counter;

    if (string9.find(filename.c_str()))		      
      ++counter;

    if (string10.find(filename.c_str()))		      
      ++counter;

    cout<< "students mark is"<<counter<<"out of 10"<<endl;	
    return EXIT_SUCCESS;						
  }
}

Changed sections of the code and an answer is now generated, only problem is is is always 10 out of ten. anythoughts

#include <cstdlib>							
#include <fstream>						
#include <iostream>
#include <cstring>
#include <string>

using namespace std;							
int main()
{
	cout << "Marking Program"<<endl;		
	string username;
	cout << "Enter Students Username:";		
	cin >> username;
	
	string filename = username + ".txt";
    ifstream file(filename.c_str());
	if (!file) 
    cerr<<"Username not found"<<endl;		
    	
    string string1 ("DDRA");
    string string2 ("DDRB"); 	
    string string3 ("0021");	
    string string4 ("CLR");
    string string5 ("LDAA");
    string string6 ("LDAB");
    string string7 ("READ");
    string string8 ("ABA");
    string string9 ("STAA");
    string string10 ("END");	
	
   int counter;
    counter=0;

if (string1.find(filename.c_str()) == string::npos)	
++counter;
         
if (string2.find(filename.c_str()) == string::npos)	
		++counter;
    	 
 if (string3.find(filename.c_str()) == string::npos)	
++counter;
    	 
    if (string4.find(filename.c_str()) == string::npos)	
		++counter;
    	 
    if (string5.find(filename.c_str()) == string::npos)	
		++counter;
    	 
    if (string6.find(filename.c_str()) == string::npos)	
		++counter;
    	 
    if (string7.find(filename.c_str()) == string::npos)	
		++counter;
    	
    if (string8.find(filename.c_str()) == string::npos)	
		++counter;
    	 
    if (string9.find(filename.c_str()) == string::npos)	
		++counter;
    	
    if (string10.find(filename.c_str()) == string::npos)	
		++counter;
    
	cout<< "students mark is"<<	counter	<<"out of 10"<<endl;	

  return EXIT_SUCCESS;
}

>if (string1.find(filename.c_str()) == string::npos)
This tests for failure. The search succeeds if the result is not string::npos.

if (string1.find(filename.c_str()) == string::npos)
Does this line search for string1 in the file, or the file in string1?

>Does this line search for string1 in the file, or the file in string1?
You wrote code and don't know what it does? The member function find is called on behalf of string1 with the argument filename.c_str(). So it's searching for filename in string1.

I got the feeling straight away that you have no idea what you're doing, and now I'm pretty sure of it. Perhaps you should work with pseudocode until you're comfortable with the design of your program. That way you won't be confused with piddling details until the time is right.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.