Menu

I am So Frustrated

Anonymous
2002-05-24
2012-09-26
  • Anonymous

    Anonymous - 2002-05-24

    I have a problem. I want my program to create a text file containing the information the people input but it just won;t for sum reason. I posted my code below any help would be greatly appreciated. Sometime C++ can be so frustrating especially for beginners

    #include <fstream.h>
    #include <iostream.h>
    #include <string.h>
    int login()
    { char nuttin[2];
      char read[10];
      char siname[10];
      char sipass[10];
      cout<<"Please Enter Your User Name:\t";
      cin>>siname;
      cout<<"Please Enter Your Password:\t";
      cin>>sipass;
      strcat(siname, ".");
      strcat(siname, sipass);
      ifstream d_file("00001.txt");
      d_file>>read;
      if (read==siname)
      { cout<<"You Have Succesfully Logged In:";
        cout<<"Press Enter To Exit!!!!:";
        cin>>nuttin;
      }
      else
      { cout<<"Your Log In Was Unsuccessful.Quit. Then Restart The Program To Try Again Or Sign Up";
        cin>>nuttin;
      }

     

    int signup()
    {
      char name[10];
      char password[10];
      char str[22];  

    cout<<"Please Create A User Name Less Then 10 Letters:\t";
    cin>>name;

    cout<<"Please Create A Password Less Then 10 Letters:\t";
    cin>>password;

    ofstream a_file("00001.txt");

    a_file << name << "." << password;
    //Outputs to text
    a_file.close();
    //Closes up the file
    ifstream b_file("00001.txt");
    //Opens for reading the file
    b_file>>str;
    cout<<str;
    //Should output the contents of the file
    b_file.close();
    //Closing the file
    }

    int main()
    { int k;
      cout<<"********Welcome to Experimant Program 1*********************";
      cout<< "\n Press 1 To Sign UP Or 2 Too Login:\t";
      cin>>k;
      if (k==2)
      { login();
      }
      else
      { signup();}
      return 0;
    }

     
    • Charles Manson

      Charles Manson - 2002-07-29

      Hello again John!

      The problem your having i one that alot of beginners have.
      I also had that problem when i was new to C++.

      The thing is that you cant compare two char-arrays in that way. Sure the compiler doesnt say anything but you dont get the result you want. What you want to do is use the
      'strcmp()' funktion like this:

      if(strcmp(read,siname)==0)
      {
              // The strings where equal
      }

      The strcmp() funktion returns 0 if the two strings are equal.

      Heres your code with the fix:

      #include <fstream.h>
      #include <iostream.h>
      #include <string.h>

      int login()
      { char nuttin[2];
      char read[10];
      char siname[10];
      char sipass[10];
      cout<<"Please Enter Your User Name:\t";
      cin>>siname;
      cout<<"Please Enter Your Password:\t";
      cin>>sipass;
      strcat(siname, ".");
      strcat(siname, sipass);
      ifstream d_file("00001.txt");
      d_file>>read;
      if (strcmp(read,siname) == 0)   // <-- FIXED !!!
      { cout<<"You Have Succesfully Logged In:";
      cout<<"Press Enter To Exit!!!!:";
      cin>>nuttin;
      }
      else
      { cout<<"Your Log In Was Unsuccessful.Quit. Then Restart The Program To Try Again Or Sign Up";
      cin>>nuttin;
      }
      }

      int signup()
      {
      char name[10];
      char password[10];
      char str[22];

      cout<<"Please Create A User Name Less Then 10 Letters:\t";
      cin>>name;

      cout<<"Please Create A Password Less Then 10 Letters:\t";
      cin>>password;

      ofstream a_file("00001.txt");

      a_file << name << "." << password;
      //Outputs to text
      a_file.close();
      //Closes up the file
      ifstream b_file("00001.txt");
      //Opens for reading the file
      b_file>>str;
      cout<<str;
      //Should output the contents of the file
      b_file.close();
      //Closing the file
      }

      int main()
      { int k;
      cout<<"********Welcome to Experimant Program1********";
      cout<< "\n Press 1 To Sign UP Or 2 Too Login:\t";
      cin>>k;
      if (k==2)
      { login();
      }
      else
      { signup();}
      return 0;
      }

      I compiled and executed this program on my computer
      using Dev-c++ 5 and it worked just fine. I created some accounts to test it with and runs perfectly.

      PS:

      Heres a little modification i made to the 'main()' function.
      Now, instead of closing the program, the main menu
      is displayed so the user can login with the new account without restarting the program.

      int main()
      {
      int k;
      bool done = false;  // True when the main loop should end

      // Loop until done
      while(!done)
      {
          cout << endl;   // Begin a new line
          cout<<"********Welcome to Experimant Program1****";
          cout<< "\n Press 1 To Sign UP Or 2 Too Login:\t";
          cin >> k;    
          if(k==3)
          {
              done = true;     // Exit the loop
          }
          else if(k==2)
          {
              login();  // Go to login screen
          }
          else if(k==1)
          {
              signup(); // Go to signup screen
          }
      }

      return 0;
      }

      Hope you like it!

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.