C++ Help Please

Discussion in 'Gaming' started by White B O I, May 12, 2009.

  1. White B O I

    White B O I Well-Known Member

    Age:
    34
    Posts:
    813
    Likes Received:
    0
    Joined:
    Apr 11, 2006
    Location:
    Table Town, Arizona
    Cant figure out why this will not compile <_<
    I'm using Bloodshed 4.9.9.2

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    struct studentType
    {
           string first[20],last[20];
    }Students;
    
    void readStudents()
    {
         string yn;
         
         cout << "Do you have a pre-made list of students (place in program directory and name it students.txt)? type Yes or No:";
         
         cin >> yn;
         
         if (yn == "yes" || yn == "Yes" || yn == "YEs" || yn == "yEs" || yn == "yeS" || yn == "yES" || yn == "YES");
         {
             ifstream in("students.txt");
             if(!in)
             {
                    cout << "Cannot open file.";
         
             exit (1);
             char str[21];
             }while(in)
             in.getline(str, 21);  
             cout << str << endl;
    }
       else
       {
           ofstream in;
           in.open ("students.txt");
           
            string input;
            for (int i=1; i<21; i++)
            {
            studentType Student;
            cout << "Enter student #" << i;
            cin >> Student.first[i];
            in << Student.first[i];
            if (i>=20)
        {
            x.close;
        }
    }
    int main(int argc, char *argv[])
    {
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    errors:
    29 C:\Documents and Settings\Admin\Desktop\main(2).cpp `str' undeclared (first use this function)
    32 C:\Documents and Settings\Admin\Desktop\main(2).cpp expected primary-expression before "else"
    32 C:\Documents and Settings\Admin\Desktop\main(2).cpp expected `;' before "else"
    53 C:\Documents and Settings\Admin\Desktop\main(2).cpp expected `}' at end of input

    help is appreciated
     
  2. Dolce

    Dolce Well-Known Member

    Posts:
    99
    Likes Received:
    0
    Joined:
    Jun 20, 2005
    I haven't used C++ since my first year of Uni, and I don't quite remember, but if(!in)... I'm not exactly sure what you're trying to do there. I'm not brilliant at the stuff, but what arguement are you trying to pass as !in, because it seems to me like it's not being recognized as an if function, and then what you have happening is the first if not ending, so then the compiler expects a ; and a } before the else to shut off the first if.

    That was probably really confusing... sorry!
     
  3. Enigma2.0

    Enigma2.0 Well-Known Member

    Age:
    30
    Posts:
    103
    Likes Received:
    0
    Joined:
    May 7, 2009
    Location:
    Tasmania
    Yeah we use this at MEgaLan i have no idea how work bloody thing always have get someone else to fix it soz
     
  4. White B O I

    White B O I Well-Known Member

    Age:
    34
    Posts:
    813
    Likes Received:
    0
    Joined:
    Apr 11, 2006
    Location:
    Table Town, Arizona
    Code:
    ifstream in("students.txt");
             if(!in)
    ifstream in declares the variable in as an input file named "students.txt"

    if(!in) means if input file is not found or an error occurs then say cannot open file

    !in means not in, and remember, in is the variable that I declared with ifstream.

    if there is a ! infront of something that means not
    example:

    Code:
    2 != 3; //2 does not equal 3
     
  5. Bleak

    Bleak Well-Known Member

    Age:
    35
    Posts:
    338
    Likes Received:
    0
    Joined:
    May 3, 2005
    Location:
    Pittsburgh
    Get rid of the semicolon, that is why you get the "else" error. Also, it is better practice to use a "toUpper" or "toLower" function instead of having all those cases. Compare 10 lines of ASM (with the to lower) to 25 lines (with your case statement), those aren't exact but just to make the point.


    Declare your "str" at the beginning... That if(!in) may not get executed and you're going to end up not having that char array declared. Even if the the condition is always met, the compiler does not assume that. That will get rid of the "str" error.

    You should format your code better, I am not sure what you want executed when a condition is met (comments would also help me and you)... That is what will fix your "}" error... You're probably missing one.


    Get rid of exit(1); ...Even if it is because you're using Bloodshed... Get Microsoft C++ Express 2008 for free.



    Found it...


    You are missing a closing bracket on your for loop. I'd just get rid of that opening brace on the if statement.

    Even with those fixed, I honestly do not believe your program will run correctly. (I noticed some logical errors)

    ----------------
    Now playing on Winamp: Entombed - Retaliation
    via FoxyTunes
     
  6. White B O I

    White B O I Well-Known Member

    Age:
    34
    Posts:
    813
    Likes Received:
    0
    Joined:
    Apr 11, 2006
    Location:
    Table Town, Arizona
    Get rid of the semicolon, that is why you get the "else" error. Also, it is better practice to use a "toUpper" or "toLower" function instead of having all those cases. Compare 10 lines of ASM (with the to lower) to 25 lines (with your case statement), those aren't exact but just to make the point.


    Declare your "str" at the beginning... That if(!in) may not get executed and you're going to end up not having that char array declared. Even if the the condition is always met, the compiler does not assume that. That will get rid of the "str" error.

    You should format your code better, I am not sure what you want executed when a condition is met (comments would also help me and you)... That is what will fix your "}" error... You're probably missing one.


    Get rid of exit(1); ...Even if it is because you're using Bloodshed... Get Microsoft C++ Express 2008 for free.



    Found it...


    You are missing a closing bracket on your for loop. I'd just get rid of that opening brace on the if statement.

    Even with those fixed, I honestly do not believe your program will run correctly. (I noticed some logical errors)

    ----------------
    Now playing on Winamp: Entombed - Retaliation
    via FoxyTunes
    [/b][/quote]

    I ended up rewriting it, it came out really clean, thanks for the help though. I have no idea why I had a ; at the end of an If statement.

    I only use bloodshed because my teacher requires it. I have Visual Studios 2008, its free from my school.

    Here's my new code just because:
    Code:
    #include <cstdlib>
    #include <iostream>
    
    
    using namespace std;
    struct studentType
    {
           string fName, lName;
           int testScore;
           char grade;
    }students[20];
    
    void Grade()
    {
          for (int i = 1; i<20; i++)
         {
             if (students[i].fName == "Finish" || students[i].fName == "finish")
             {
                  i = 20;
             }
            else if (students[i].testScore>89)
                students[i].grade ='A';
            else if (students[i].testScore>79 && students[i].testScore<=89)
                students[i].grade ='B';
    
            else if (students[i].testScore>69 && students[i].testScore<=79)
                students[i].grade ='C';
            else if (students[i].testScore>59 && students[i].testScore<=69)
                students[i].grade ='D';
    
            else 
                 students[i].grade ='F';
    
    
             }
    }
    void Input()
    {
         
         cout << "Please insert students First name, Last name, and test score.\n\n";
         cout << "Example: John Smith 89\n";
         cout << " first name^ last^  ^score\n\n";
         cout << "Type \"Finish\" when you are completed.\n\n";
        
         
         for (int i=1; i<=20; i++)
         {
             cin >> students[i].fName;
             if (students[i].fName == "Finish" || students[i].fName == "finish")
             {
                                   i = 20;
             }
             else
                 cin >> students[i].lName >> students[i].testScore;
                 cout << endl;
             }
         }
         
    void highScore()
    {
         string first = students[0].fName, last = students[0].lName;
         int highScore = students[0].testScore;
         char grade = students[0].grade;
         
         for (int i = 1; i<20; i++)
         {
             if (students[i].fName == "Finish" || students[i].fName == "finish")
             {
                                   i = 20;
             }
             else if (students[i].testScore > highScore)
             {
                  highScore = students[i].testScore;
                  first = students[i].fName;
                  last = students[i].lName;
                  }
             }
             cout << "Highest score: " << last << ", " << first << " " << grade << " " << highScore << endl;
         
    }
    
    void Output()
    { 
         for (int i = 1; i<20; i++)
         {
             if (students[i].fName == "Finish" || students[i].fName == "finish")
             {
                                   i = 20;
             }
             else
             {
                 cout << students[i].lName << ", " << students[i].fName << " " << students[i].grade << " " << students[i].testScore << endl;
             }
         }
          highScore();
    }     
    int main(int argc, char *argv[])
    {
        Input();
        Grade();
        Output();
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
     

Share This Page