944,065 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2981
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 17th, 2005
0

Program with many errors

Expand Post »
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)";

}

}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jonnie83 is offline Offline
24 posts
since Feb 2005
Feb 18th, 2005
0

Re: Program with many errors

Reputation Points: 14
Solved Threads: 4
Junior Poster
prog-bman is offline Offline
108 posts
since Nov 2004
Feb 18th, 2005
0

Re: Program with many errors

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:
C++ Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2. #include <fstream>
  3. #include <iostream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. ifstream in("somefile");
  11. string search;
  12. string line;
  13. int n = 0;
  14.  
  15. if (!in) {
  16. cerr<<"Error opening file"<<endl;
  17. return EXIT_FAILURE;
  18. }
  19.  
  20. cout<<"Enter a search string: ";
  21. if (!getline(cin, search)) {
  22. cerr<<"Invalid input"<<endl;
  23. return EXIT_FAILURE;
  24. }
  25.  
  26. while (getline(in, line)) {
  27. if (line.find(search) == string::npos)
  28. ++n;
  29. }
  30.  
  31. cout<< n <<" occurances of "<< search <<" were found"<<endl;
  32.  
  33. return EXIT_SUCCESS;
  34. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Feb 21st, 2005
0

Re: Program with many errors

Thanks for your help, I really was stuck
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jonnie83 is offline Offline
24 posts
since Feb 2005
Feb 24th, 2005
0

Re: Program with many errors

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;
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jonnie83 is offline Offline
24 posts
since Feb 2005
Feb 24th, 2005
0

Re: Program with many errors

>still having problems
Can you be more specific?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Feb 24th, 2005
0

Re: Program with many errors

sorry forgot code tags

C++ Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2. #include <fstream>
  3. #include <iostream>
  4. #include <string>
  5.  
  6. using namespace std;
  7. using std::cout;
  8. using std::cin;
  9. using std::ifstream;
  10.  
  11. int main()
  12. {
  13. string username; //Username that will be searched for
  14.  
  15. cout << "Marking Program"; //Start of program
  16.  
  17. cout << "Enter Students Username:"; //Get username off program user
  18. cin >> username;
  19.  
  20. ifstream in("checkUserName.txt"); //Searching for students username
  21. string search;
  22. string line;
  23. int n = 0;
  24.  
  25. if (!in) {
  26. cerr<<"Username not found"<<endl; //If entered username not found display
  27. return EXIT_FAILURE; //error message
  28. }
  29. else
  30. if (in){
  31. ifstream in(username.txt); //open students program
  32. }
  33.  
  34.  
  35.  
  36.  
  37. { ifstream in("markScheme.txt"); //open the marking scheme file
  38. if (!in){
  39. cerr<<"Mark Scheme not found"<<endl; //If file not found display error
  40. return EXIT_FAILURE;
  41. }
  42. }
  43.  
  44.  
  45.  
  46. if (!getline(cin, search)) {
  47. !=++n;
  48. }
  49.  
  50. while (getline(in, line)) {
  51. if (line.find(search) == string::npos)
  52. ++n;
  53. }
  54.  
  55. cout<< "students mark is"<<n<<"out of 10"<<endl;
  56.  
  57. return EXIT_SUCCESS;
  58. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jonnie83 is offline Offline
24 posts
since Feb 2005
Feb 24th, 2005
0

Re: Program with many errors

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jonnie83 is offline Offline
24 posts
since Feb 2005
Feb 24th, 2005
0

Re: Program with many errors

It makes perfect sense. What I want to know is what the program you have is doing that fails to meet your expectations.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Feb 24th, 2005
0

Re: Program with many errors

Once a string is found (10 in total) i want it to increment the counter then output the final mark to the program user
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jonnie83 is offline Offline
24 posts
since Feb 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: help with file streaming
Next Thread in C++ Forum Timeline: Help referencing variables





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC