Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

Another c++ cheery pop

Finished making your own program (thats not part of the practicing forums) ? Show it off here

Another c++ cheery pop

Postby Sneakyfeet on Sat Oct 03, 2009 10:35 am

Heya

So after watching the so far 6-something videos of antiRTFM I finally grabbed my compiler and tried to make something...
This is solely a knowledge test, so it was something like trying out as many of the things I could find room for - more or less.

The idea was to create some silly ez to use account creator. It can store 5 different accounts with a match of a Username and a Password,
and recognize one of these upon a "log in", and of course a way to exit.

Besides antiRTFM's videos, I took a deeper look arrays and explored the possibilities of strings.

Try not to care to much for the tons of comments... only made em to reasure myself of what I was doing, and might be able to understand it later on :D

Code: Select all
/* My first program, yay:D
   With the goal of making an account creating and accessing test to 5 different possible accounts.
   Trying out knowledge of c++ gained through antiRTFM's spoonfeed tut vids:D + a bit here and there
*/

#include "stdafx.h"  //some stupid Visual Studio must-have include...
#include <iostream>  //iostream is pretty handy (input/output-stream)
#include <string>    //string for chains of characters, also pretty handy
#include "Account.h" //the account handling class

using namespace std;

void Menu(char *,bool *,Account*);  //function decleration for handling the menu with an input
void LogIn(string*,string*,Account*);  //function decleration for handling Log in sub menu
void CrtNewAcc(string*,string*,Account*);  //function decleration for creating a new account

int main(void)
{
   Account User[5];  //making and array of type Account with ADRESS User (5 spots means 5 possible accounts)
   char UserInput1;  //small variable for Menu choise
   bool Exit=false;  //boolean for exiting the program

   while(true)  //"going until break"-loop
   {
      Menu(&UserInput1,&Exit,User);  //function call for handling user input and the reactions

      if (Exit)  //Its t3h 0nly wai out! o.O
         break;
   }
   
   return 0; //end of main -> end of program
}

void Menu(char * UserInput1, bool *Exit, Account *User) //function definition wich takes POINTERS!
{   
   string Username, Password;

   cout << "Welcome to the account menu\n" << "\n1 - Log In\n" << "2 - create new account\n"
      << "3 - Quit\n" << "please enter your menu choise: ";      //a greeting
   cin >> *UserInput1;  //get input

   system("cls");  //clear screen
   switch (*UserInput1)  //testing input for menu choise 
   {
   case '1':
      LogIn(&Username,&Password,User);  //1=Log In mode
      break;
   case '2':
      CrtNewAcc(&Username,&Password,User);  //2=create new account mode
      break;
   case '3':
      cout << "Byebye... Cya some other time:D\n";  //3 = exit
      *Exit=true;  //pointers work!
      break;
   default:
      cout << "You've typed something illegal... shame on you!\n\n";  //for the retarded people
      break;
   }
}

void LogIn(string *Username, string *Password, Account *User) //another function definition with alot of adresses
{
   bool LoginSuceed=false;  //variable to keep track on succesfull log in attempt

   cout << "Welcome to Login Page!\n";
   cout << "Please type in your username: ";  //explainin text
   cin >> *Username;  //point4r!      (dereferencing with the "*" to access and alter location content
   cout << "\nplease type in your password: ";
   cin >> *Password;  //moar point4r!
   for (int i=0;i<5;i++) //Much love to for loops when accessing arrays
   {                  //goes through and checks every User for a match
      if(*Username==User[i].GetUsername()&&*Password==User[i].GetPassword())
      {                                                      
         cout << "\nCongratulations, you've succesfully logged in!:D\n\n\n";  //sucessfull login
         LoginSuceed=true;
      }
      if(LoginSuceed)
         break;  //no need to keep on looking for a match when match was found
   }
   if(!LoginSuceed)  //failed to find a match and ran through all Users
      cout << "you've entered an invalid username and password combination...\n\n\n";
}

void CrtNewAcc(string *Username, string *Password, Account *User) //function definition for the create new account
{
   cout << "Welcome to \"Create new Account\"!\n"; 
   cout << "Please type, desired username: ";  //txt
   cin >> *Username;  //something about working with the "original" variable in a different function just
   cout <<"\nNow please enter the desired password: ";  //feels right...
   cin >> *Password;  //EVEN MOAR POINT4R!
   cout <<"\nYour account has been created and is ready for login!\n\n\n";  //using \n to make the program look
   for(int i=0;i<5;i++)                                       //more accesable
   {   //t3h cr34t0r! ...vowels are for pros
      if(User[i].GetUsername()=="1")
         {
            User[i].ChangePassword(*Password);  //Important self note: The name of an array, ie User
            User[i].ChangeUsername(*Username);  //can be considered an adress of the rest of the array
            break;                        //therefore it works sweet with pointers, which stores
         }                              //adresses
   }
}


With header:
Code: Select all
//Account Header file
#include "stdafx.h"
#include <iostream>
#include <string>


class Account
{
public:
   Account();   //default constructor decleration
   Account(std::string,std::string); //construct decleration
   std::string GetPassword(); //method declaration for returning member variable Password
   std::string GetUsername(); //method declaration for returning member variable Username
   void ChangePassword(std::string); //method declaration for changing member variable Password
   void ChangeUsername(std::string); //method declaration for changing member variable Username
private:
   std::string Username; //member string-variable Username
   std::string Password; //member string-variable Password
};


and additional cpp:
Code: Select all
//Account source file
#include "stdafx.h"
#include "Account.h"

using namespace std;

Account::Account():Username("1"),Password("2")  {}  //default constructor


Account::Account (string AUsername,string APassword):  //constructor
  Username(AUsername), Password(APassword)  {}


string Account::GetPassword() //method to give Password string
{
   return Password;
}

void Account::ChangePassword(string APassword) //method to change Password
{
   Password = APassword;
}

void Account::ChangeUsername(string AUsername) //methid to change Username
{
   Username = AUsername;
}
string Account::GetUsername()   //method to give Username
{
   return Username;
}


The only purpose of this post would be if any could point out some warnings, as in... You should try to avoid coding namespaces in headers (as some wierd dude informed me) or maybe some suggestions for how I could avoid a couple of lines of code by doing this'n'that... might aswell get into some good habbits straight away :!:

As with any cherry pop, be gentle :lol:

EDIT: after a quick lesson on references by noobgrammer in the chat-room, it seems that I could've used ref instead of pointers most of the way
Sneakyfeet
 
Posts: 1
Joined: Wed Sep 30, 2009 2:34 pm

Return to Full programs

Who is online

Users browsing this forum: No registered users and 0 guests