passing bye parts of program

This is a discussion on passing bye parts of program within the C++ Programming forums, part of the General Programming Boards category; in a currecnt program i am working on, i have places where the user would enter there first and last ...

  1. #1
    C++No0b!!!
    Join Date
    Jul 2005
    Location
    penn
    Posts
    66

    passing bye parts of program

    in a currecnt program i am working on, i have places where the user would enter there first and last name. when i run the program, it completly passes bye that part and fills in all the other fields aswell. and then later when the name is ouputed it has no value.
    here is the part of the code:
    Code:
    cout << "\n\nFirst Name: ";
         cin.get(V.FirstName,20);
         cin.ignore(80,'\n');
                          
         V.FirstName[0]=toupper(V.FirstName[0]);
                          
         cout << "\nLast Name: ";
         cin.get(V.LastName,30);
         cin.ignore(80,'\n');
                          
         V.LastName[0]=toupper(V.LastName[0]);
                     
         strcpy(V.FullName, V.FirstName);
         strcat(V.FullName, " ");
         strcat(V.FullName, V.LastName);
    as you can see i use the cin.get and cin.ignore since they are character strings and i am using a struct which is why there is a V. and then variable name

    also this is the first program i passed structs from one function to another

    here is the struct:
    Code:
    struct Variables
    {
       char FirstName[20], LastName[30], FullName[50], Password[10], PW[10], User, Print, acnumber[8];
       int Attempts;
       float Balance, Deposits, Withdrawls, NewBalance;
    };
    and here's main if you need it:
    Code:
    int main ()
    {
       Variables V;
       
       V.Attempts=0;
       V.Balance=0;
       V.Deposits=0;
       V.Withdrawls=0;
       V.NewBalance=0;
       
       Intro(V);
       
       AccountNumber(V);
     
       createorisregistered(V); 
         
       printcorrect(V);
       
       getch();
       return 0;
    }
    can anyone explain why these things are being passed bye wen running the program? thanks for your help in advance.

  2. #2
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    is that part of code being called by main()?
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    It seems to work for me. This code produces the following output:
    Code:
    #include <iostream>
    using namespace std;
    
    struct Variables
    {
       char FirstName[20], LastName[30], FullName[50], Password[10], PW[10], User, Print, acnumber[8];
       int Attempts;
       float Balance, Deposits, Withdrawls, NewBalance;
    };
    
    
    int main ()
    {
    	Variables V;
    
    	V.Attempts=0;
    	V.Balance=0;
    	V.Deposits=0;
    	V.Withdrawls=0;
    	V.NewBalance=0;
    
    	cout << "\n\nFirst Name: ";
    	cin.get(V.FirstName,20);
    	cin.ignore(80,'\n');
                  
    	V.FirstName[0]=toupper(V.FirstName[0]);
                  
    	cout << "\nLast Name: ";
    	cin.get(V.LastName,30);
    	cin.ignore(80,'\n');
                  
    	V.LastName[0]=toupper(V.LastName[0]);
             
    	strcpy(V.FullName, V.FirstName);
    	strcat(V.FullName, " ");
    	strcat(V.FullName, V.LastName);
    
    	cout<<V.FirstName<<endl;
    	cout<<V.LastName<<endl;
    	cout<<V.FullName<<endl;
    
    	return 0;
    }
    output:
    Code:
    First Name: tom
    
    Last Name: smith
    Tom
    Smith
    Tom Smith
    Press any key to continue

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Make sure you declare your function to pass the class or struct by reference (&) when the function you are passing it to needs to modify it:
    Code:
       void createorisregistered(Variables &V)
    For function printcorrect(), you can just pass by value, although since your struct has several members, passing by reference might be better in this case also:
    Code:
    void printcorrect(Variables &V)

  5. #5
    C++No0b!!!
    Join Date
    Jul 2005
    Location
    penn
    Posts
    66
    i think i am passing by reference...

    heres all the code if it helps:
    Code:
    //Dan Kemper
    //Intermediate Computer Programming
    //This is a bank program
    
    //includes header files
    #include <conio.h>
    #include <iostream>	
    #include <fstream>	
    #include <iomanip>
    #include <windows.h>
    #include <cctype>	
    
    //required for cout and cin
    using namespace std;	
    
    struct Variables
    {
       char FirstName[20], LastName[30], FullName[50], Password[10], PW[10], User, Print, acnumber[8];
       int Attempts;
       float Balance, Deposits, Withdrawls, NewBalance;
    };
    
    void Intro(Variables &V);
    
    void AccountNumber(Variables &V);
    
    void SignIn(Variables &V);
    
    void ifcorrect(Variables &V);
    
    void createorisregistered(Variables &V);
    
    void ifincorrect(Variables &V);
    
    void createaccount(Variables &V);
    
    void printcorrect(Variables &V);
    
    int main ()
    {
       Variables V;
       
       V.Attempts=0;
       V.Balance=0;
       V.Deposits=0;
       V.Withdrawls=0;
       V.NewBalance=0;
       
       Intro(V);
       
       AccountNumber(V);
     
       createorisregistered(V); 
         
       printcorrect(V);
       
       getch();
       return 0;
    }
    
    void Intro(Variables &V)
    { 
       cout << "       AAA         TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT\n";
       cout << "      AAAAA        TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT\n";
       cout << "     AAA AAA            TTTTTT      MMMMMM         MMMMMM\n";
       cout << "    AAA   AAA           TTTTTT      MMM MMM       MMM MMM\n";
       cout << "   AAAAAAAAAAA          TTTTTT      MMM  MMM     MMM  MMM\n";
       cout << "  AAAAAAAAAAAAA         TTTTTT      MMM   MMM   MMM   MMM\n";
       cout << " AAA         AAA        TTTTTT      MMM    MMM MMM    MMM\n";
       cout << "AAA           AAA       TTTTTT      MMM     MMMMM     MMM\n";
       cout << "\nThis is a bank program.\n";
       cout << "\nPress [ENTER] to continue...\n";
       cin.get();
       system("cls");
       
       cout << "Are you a registered user (A), or would you like to create an account (B)? ";
       cin >> V.User;
       system("cls");
    }
    
    void createorisregistered(Variables &V)
    {
       if (V.User=='a' || V.User=='A')
       {
          SignIn(V);
                     
                     if (V.Password==V.PW)
                     {
                        ifcorrect(V);
                     }
                     if (V.Password!=V.PW)
                     {
                        ifincorrect(V);
                     }
       }
       
       if (V.User=='b' || V.User=='B')
       {
          createaccount(V);
       }
    }
    
    void SignIn(Variables &V)
    {     
         ifstream infile; //declares file pointer named infile
         infile.open("Password.dat",ios::in); //opens dat file which has password in it
         infile.get(V.PW,10); //gets password from file
         infile.ignore(80, '\n');
         infile.close();
                     
         infile.open("Balance.dat",ios::in);
                     
         cout << "First Name: ";
         cin.get(V.FirstName,20);
         cin.ignore(80,'\n');
    
         V.FirstName[0]=toupper(V.FirstName[0]);
                     
         cout << "\nLast Name: ";
         cin.get(V.LastName,30);
         cin.ignore(80,'\n');
                     
         V.LastName[0]=toupper(V.LastName[0]);
                     
         strcpy(V.FullName, V.FirstName);
         strcat(V.FullName, " ");
         strcat(V.FullName, V.LastName);
                     
         system("cls");
                     
         cout << V.FullName << ", what is your password: ";
         cin.get(V.Password,10);
         cin.ignore(80, '\n');
         cout << V.PW; 
         getch();
         system("cls");
    }
    
    void ifcorrect(Variables &V)
    {
         cout << "Account Number: " << V.acnumber << "\n";
         cout << "Balance: " << V.NewBalance;
         cout << "\n\nDeposits: ";
         cin >> V.Deposits;
         cout << "\n\nWithdrawls: ";
         cin >> V.Withdrawls;
         cout << "\n\nNew Balance: " << V.NewBalance;
         cout << "\n\n" << V.FullName << ", is all the above information correct (Y/N)? ";
         cin >> V.Print;
    }
    
    void ifincorrect(Variables &V)
    {
         do
         {
             cout << "\n\nThat was the incorrect password!\a\a\a";
             getch();
             system("cls");
             cout << "Try again: ";
             cin >> V.Password;
             if (V.Password==V.PW)
             {
                  break;
             }
             if (V.Attempts==3)
             {
             cout << "I'm sorry, but you have made too many incorrect attempts. Please try again later.";
             }
             V.Attempts++;
             }
             while (V.Attempts<=3);
    }
    
    void createaccount(Variables &V)
    {
         cout << "Your account number will be: " << V.acnumber;
         cout << "\n\nFirst Name: ";
         cin.get(V.FirstName,20);
         cin.ignore(80,'\n');
                          
         V.FirstName[0]=toupper(V.FirstName[0]);
                          
         cout << "\nLast Name: ";
         cin.get(V.LastName,30);
         cin.ignore(80,'\n');
                          
         V.LastName[0]=toupper(V.LastName[0]);
                     
         strcpy(V.FullName, V.FirstName);
         strcat(V.FullName, " ");
         strcat(V.FullName, V.LastName);
                          
         system("cls");
                          
         cout << V.FullName << ", pick your password and don't forget it: ";
         cin.get(V.Password, 10);
         cin.ignore(80, '\n');
         cout << "\nAccount Number: " << V.acnumber;
         cout << "\nDeposits: ";
         cin >> V.Deposits;
         cout << "\nWithdrawls: ";
         cin >> V.Withdrawls;
                          
         V.NewBalance=V.Balance+V.Deposits-V.Withdrawls;
                          
         cout << "\nBalance: ";
         cout << setiosflags (ios::fixed) << setprecision(2) << V.NewBalance ;
         cout << "\n\n" << V.FullName << ", is all the above information correct (Y/N)? ";
         cin >> V.Print;
    }
    
    void printcorrect(Variables &V)
    {
         if (V.Print=='y' || V.Print=='Y') 
         { 
         ofstream outfile ("Bank.txt");
         outfile << V.FullName;
         outfile << "\nPassword: *******";
         outfile << "\n\nDeposits: " << V.Deposits;
         outfile << "\nBalance: " << V.Deposits;
         outfile << "\nThank you!";
         outfile.close();
         ofstream  pwfile ("Passwords.dat");
         pwfile << V.Password;
         pwfile.close();
         ofstream balancefile ("Balance.dat");
         balancefile << V.NewBalance; 
         cout << "\n\nThank You!\a";
         }
    }
    
    void AccountNumber(Variables &V)
    {
       srand((unsigned)time(0)); 
       for ( int i = 0; i < 8; i++ )
       {
          V.acnumber[i] = rand() % 10 + '0';
       }
       V.acnumber[7] = '\0';
    }

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > cin >> V.User;
    This cin is going to leave a newline in the input buffer, and since you're using get()'s shortly thereafter, the get()'s are going to read the newline, then continue. So you probably want to add a cin.ignore right after the above:
    Code:
       cin >> V.User;
       cin.ignore(80, '\n');

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. redirection program help needed??
    By Unregistered in forum Linux Programming
    Replies: 0
    Last Post: 04-17-2002, 06:50 AM
  5. Clocking a program, and parts of it
    By Boksha in forum C Programming
    Replies: 4
    Last Post: 03-18-2002, 06:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21