This is my first attempt at creating a program and I am having great problems. The program is meant to scan a file and increment a counter if specific words are found. Can anyone let me know where I'm going wrong,Thanks

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <conio.h>

using namespace std;

int main (void)
{
    string string1;
    int location;
    char userName;  //The students log-in name
    int markCounter //Counter for adding up students mark



    system("cls");  // Clear the screen

    cout << "Marking Program"  << endl << endl;

    ifstream infile("checkUserName.txt", ios::in);   

    //Get students username
    cout << "Enter Students Username: ";
    cin >>string1;// entered username


    // Check students username validity
    userName.find_first_of("checkUserName.txt");

    if string1 /found then
        cout << "Username not found"<<;

    else
        ifstream infile("string1.txt", ios::in); //open students file
        ifstream infile("markScheme.txt", ios::in);

    while (getline(markScheme, line1)) {

        if (line1.find(search) != string::npos)
    ++1 counter;

        while (getline(markScheme, line2)) {

        if (line2.find(search) != string::npos)
    ++1 counter;

        while (getline(markScheme, line3)) {

        if (line3.find(search) != string::npos)
    ++1 counter;

        while (getline(markScheme, line4)) {

        if (line4.find(search) != string::npos)
    ++1 counter;

        while (getline(markScheme, line5)) {

        if (line5.find(search) != string::npos)
    ++1 counter;

        while (getline(markScheme, line6)) {

        if (line6.find(search) != string::npos)
    ++1 counter;

        while (getline(markScheme, line7)) {

        if (line7.find(search) != string::npos)
    ++1 counter;

        while (getline(markScheme, line8)) {

        if (line8.find(search) != string::npos)
    ++1 counter;

        while (getline(markScheme, line9)) {

        if (line9.find(search) != string::npos)
    ++1 counter;

        while (getline(markScheme, line10)) {

        if (line10.find(search) != string::npos)
    ++1 counter;


    //Display students mark 
    cout << "Students mark is (counter)";

        }

}

Recommended Answers

All 14 Replies

I'm not surprised you're getting errors. Aside from the odd choice of using so many loops where one will do, you fail to close most of your blocks. Compare this with what you have and do try to learn from it:

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

using namespace std;

int main()
{
  ifstream in("somefile");
  string search;
  string line;
  int n = 0;

  if (!in) {
    cerr<<"Error opening file"<<endl;
    return EXIT_FAILURE;
  }

  cout<<"Enter a search string: ";
  if (!getline(cin, search)) {
    cerr<<"Invalid input"<<endl;
    return EXIT_FAILURE;
  }

  while (getline(in, line)) {
    if (line.find(search) == string::npos)
      ++n;
  }

  cout<< n <<" occurances of "<< search <<" were found"<<endl;

  return EXIT_SUCCESS;
}

Thanks for your help, I really was stuck

still having problems using the strings created in the marking scheme being searched for in the students file this is what i have got so far

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


using namespace std;
using std::cout;
using std::cin;
using std::ifstream;


int main()
{
string username;                        //Username that will be searched for


cout << "Marking Program";                //Start of program


cout << "Enter Students Username:";       //Get username off program user
cin >> username;


ifstream in("checkUserName.txt");       //Searching for students username
string search;
string line;
int n = 0;


if (!in) {
cerr<<"Username not found"<<endl;       //If entered username not found display
return EXIT_FAILURE;                    //error message
}
else
if (in){
ifstream in(username.txt);          //open students program
}



{ ifstream in("markScheme.txt");        //open the marking scheme file
if (!in){
cerr<<"Mark Scheme not found"<<endl;    //If file not found display error
return EXIT_FAILURE;
}
}


if (!getline(cin, search)) {
!=++n;
}


while (getline(in, line)) {
if (line.find(search) == string::npos)
++n;
}


cout<< "students mark is"<<n<<"out of 10"<<endl;


return EXIT_SUCCESS;
}

>still having problems
Can you be more specific?

sorry forgot code tags

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

using namespace std;
using std::cout;
using std::cin;
using std::ifstream;

int main()
{
	string username;						//Username that will be searched for
	
	cout << "Marking Program";				//Start of program

	cout << "Enter Students Username:";		//Get username off program user
	cin >> username;

	ifstream in("checkUserName.txt");		//Searching for students username
	string search;
	string line;
	int n = 0;

	if (!in) {
    cerr<<"Username not found"<<endl;		//If entered username not found display
    return EXIT_FAILURE;					//error message
	}
	else 
		if (in){
		ifstream in(username.txt);			//open students program
	}
	
	
	
	
	{ ifstream in("markScheme.txt");		//open the marking scheme file
	if (!in){
	cerr<<"Mark Scheme not found"<<endl;	//If file not found display error
	return EXIT_FAILURE;
	}
	}



    if (!getline(cin, search)) {
    !=++n;
  }

  while (getline(in, line)) {
    if (line.find(search) == string::npos)
      ++n;
  }

  cout<< "students mark is"<<n<<"out of 10"<<endl;

  return EXIT_SUCCESS;
}

yeah what i want the program to do is use the strings i've created in the markScheme.txt to see if they are in the students file that I open. Does that make sense!

It makes perfect sense. What I want to know is what the program you have is doing that fails to meet your expectations.

Once a string is found (10 in total) i want it to increment the counter then output the final mark to the program user

Here's where I stand. I haven't looked at your most recent code. I shouldn't need to look at your code to understand your problem. I don't give a rats ass what you want the program to do. I figured that out a long time ago. What I want to know is what your program is doing right now so that I can have a good idea of what's going wrong before I start fiddling with your code. 9 times out of 10, if I know what the program is doing and what you want the program to do, I can solve the problem without even compiling the code given.

Sure, I could take the time to compile and run your program, go through the code, guess at how you were trying to do things, guess at your intentions, and come up with a passable answer. But in the end that wastes my time and doesn't help you nearly as much as if you had asked a good question to begin with.

I'm not trying to be mean, I just don't have a lot of time to spend helping you. I want you to get the most out of this, so I need to give you a lot of bang in very little time.

ok sorry! its not opening the students file and its not marking it

Okay! That's more like it. :) So it's printing "Mark Scheme not found" or "Username not found"? How are the files formatted? Can you post a sample of both files so that I can play with your code?

Its not compiling due to these problems, sorry first attempt at using C++,
this is an example of the mark scheme DDRA
PORTA
CLR
LDAA #$FF
LDAA
LDAB
READ
ABA
STAA
END

and this is an example of a student program

; Configure ports used

DDRA EQU 0002
DDRB EQU 0003
PORTA EQU 0000
PORTB EQU 0001
DDRH EQU 0021
PORTH EQU 0020

;Start of program

CLR DDRA ;Set port A as input
CLR DDRB ;Set port B as input
LDAA #$FF ;Set port H as output
STAA DDRH ;Output value to port H
READ LDAA PORTA ;Load value from port A into reg A
LDAB PORTB ;Load value from port B into reg B
ABA ;Add reg A to reg B
STAA PORTH ;Output result to port H
end ;End of Program

may I ask why are you using :

using namespace std; AND using std::cout;
using std::cin;
using std::ifstream;?

I thought they all belong to the std family if I'm right then surely u can delete those??

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.