Hello again.
I have another conundrum on my hands. At the moment I am trying to program a simple flowchart of an ordering system of sorts. At one part, the program will ask the user for his/her name, then continue on. Later on, the user will place an order, and at the end the program will output all the previously inputted info in an invoice.

Unfortunately this is where I have hit a snag as I have no idea how do to so. I've tried tinkering around with the & pointer with no success. My prior experience is just making simple void functions, so a function that returns input later on is kind of new to me.

Would appreciate any help on this matter.

Edited 5 Years Ago by Celcion: n/a

I've tried tinkering around with the & pointer with no success.

That's like saying you're trying to solve a word problem and you've tinkered with the + operator. While it might be relevant, the connection is tenuous and impossible to see from an outsider's perspective.

Presumably you're having issues taking input in one function and returning that input to the caller. The two most common options are a return value and an output parameter. The return value is preferred if you're only returning a single item because it's usually easier to understand the control flow and more concise for the caller:

#include <iostream>
#include <string>

std::string foo()
{
    std::string input;
    
    getline(std::cin, input);
    
    return input;
}

int main()
{
    std::string saved = foo();
    
    std::cout << "saved: '" << saved << "'\n";
}

Output parameters use references (or pointers in rare cases) to assign to an object that's passed into the function:

#include <iostream>
#include <string>

void foo(std::string& input)
{
    getline(std::cin, input);
}

int main()
{
    std::string saved;
    
    foo(saved);
    
    std::cout << "saved: '" << saved << "'\n";
}

Output parameters can simplify the callee's function body at the cost of making the caller's life harder. The biggest benefit of output parameters is you aren't restricted to a single object like with return values.

These are generic examples. If you want more specific help, you'll need to ask a more specific question and show some of your own code.

Thanks for your reply.
Sorry for being vague, I guess I could say I was looking for generic examples.

Here's my code, although some of it still needs working on.

#include <iostream>
#include <string>
#include <vector>
#include <iomanip>

using namespace std;

//Entering protofunctions first
void UserVerif ();
void UserCreate();
void PayVerif ();
void NameVerif();
void OrderVerif ();
void OrderFinal (); 

struct ExistUserRecord
{
       public:
       string name;
       string address;
       string phone;
       ExistUserRecord(string a, string b, string c)
       {
          name    = a;
          address = b;
          phone   = c;
       }
};

struct NewUserRecord
{
      public:       
      string name;
      string address;
      string phone;
      NewUserRecord(string a, string b, string c)
       {
          name    = a;
          address = b;
          phone   = c;
       }
};

//Product class
struct ProdRecord
{
      public:
      string name;
      float price;
      int stock;
      ProdRecord(string a, float b, int c)
      {
         name  = a; 
         price = b; 
         stock = c;
      }
};

//User account verif. function
void UserVerif ()
{
     int userin;
     bool uVerif = false;
     
     while (uVerif == false)
     {
         cout << "Before placing your order, do you have an existing user account?" << endl;
         cout << "1. Yes" << endl;
         cout << "2. No" << endl;
         cout << "Please enter 1 or 2 for your account status." << endl;
         cin >> userin;
      
         if ((userin < 1) || (userin > 2))
         {
            cout << "Please enter either 1 or 2." << endl;
            system("pause");
            system("cls");
         }
         else
         uVerif = true;
     }
      if (userin == 1)      NameVerif();  //go to account name verif. function
      else if (userin == 2) UserCreate(); //go to account creation function
}


//New user creation function
void UserCreate()
{
     vector<NewUserRecord>users;
     
     string name;
     string address;
     string phone;
    
     NewUserRecord *u1;
     
     system("cls");
     cout << "Please enter your full name." << endl;
     cin >> name;
     cout << "Please enter your address (Only 1 line)." << endl;
     cin >> address;
     cout << "Please enter your phone number." << endl;
     cin >> phone;
     
     users.push_back(NewUserRecord(name, address, phone));
     PayVerif(); //after creating account go to pay verification
}


//account name verif. function
void NameVerif ()
{
     string namein;
     vector<ExistUserRecord>users;
    
     users.push_back(ExistUserRecord("Adam", "14 Alpha Street", "013-4897412"));
     users.push_back(ExistUserRecord("Bob", "18 Beta Avenue", "016-7513547"));
     users.push_back(ExistUserRecord("Carol", "9 Zeta Lane", "019-1355497"));
     
     system("cls");
     cout << "Please enter your full name for us to verify your account." << endl;
     cin >> namein;
     
     if (namein == users[0].name)
     {
     cout << "Welcome back, Adam." << endl;
     system("pause");
     PayVerif();
     }
     if (namein == users[1].name)
     {
     cout << "Welcome back, Bob." << endl;
     system("pause");
     PayVerif();
     }
     if (namein == users[2].name)
     {
     cout << "Welcome back, Carol." << endl;
     system("pause");
     PayVerif();
     }
     else
     {
     int namefailin;
     bool nVerif = false;
     
     while (nVerif == false)
           {
              system("cls");     
              cout << "We could not find your name in the database." << endl;
              cout << "Please enter the following numbers for your next choice." << endl;
              cout << "1. Re-enter your name." << endl;
              cout << "2. Create a new user account." << endl;
              cin >> namefailin;
            
              if ((namefailin < 1) || (namefailin > 2))
                 {
                   cout << "Please enter either 1 or 2." << endl;
                   system("pause");
                   system("cls");
                 }
              else
              nVerif = true;
           }
           if (namefailin == 1)      NameVerif();
           else if (namefailin == 2) UserCreate();
     }

     system("pause");
     PayVerif();
}


//paypal verif. function
void PayVerif()
{
     int payin;
     bool pVerif = false;
     
     while (pVerif == false)
     {
           system("cls");
           cout << "Are you paying via:" << endl;
           cout << "1. Credit card" << endl;
           cout << "2. Paypal" << endl;
           cout << "3. Neither" << endl;
           cout << "Please enter 1,2 or 3 for your payment choice." << endl;
           cin >> payin;
           
           if ((payin < 1) || (payin > 3))
           {
                cout << "Please enter a number between 1 and 3." << endl;
                system("pause");
                system("cls");
           }
           
           else
           pVerif = true;
     }     
           if      (payin == 1) OrderVerif(); //move to order taking function
           else if (payin == 2) OrderVerif(); //move to order taking function
           else if (payin == 3)
           {
                system("cls"); 
                cout << "I'm sorry, but our service only accepts credit cards or Paypal as a payment choice." << endl;    
           }
}


//order taking function
void OrderVerif()
{
     int ordertin;
     bool oVerif = false;
     
     while (oVerif == false)
     {
       vector<ProdRecord>products;
       
       products.push_back(ProdRecord("Diablo 3", 150.00, 0));
       products.push_back(ProdRecord("Call of Duty 5", 150.00, 20));
       products.push_back(ProdRecord("Starcraft 3", 150.00, 30));
       products.push_back(ProdRecord("Battlefield 4", 150.00, 30));
       products.push_back(ProdRecord("Warcraft 4", 150.00, 25));
       products.push_back(ProdRecord("Catz 'N Dogz", 50.00, 100));
     
       system("cls");
       cout << fixed << setprecision(2);
       cout << "Please select your desired product:" << endl;
       cout << "1. " << setw(18) << left << products[0].name << "RM" << setw(10) << left << products[0].price << "STOCK= " << products[0].stock << endl;
       cout << "2. " << setw(18) << left << products[1].name << "RM" << setw(10) << left << products[1].price << "STOCK= " << products[1].stock << endl;
       cout << "3. " << setw(18) << left << products[2].name << "RM" << setw(10) << left << products[2].price << "STOCK= " << products[2].stock << endl;
       cout << "4. " << setw(18) << left << products[3].name << "RM" << setw(10) << left << products[3].price << "STOCK= " << products[3].stock << endl;
       cout << "5. " << setw(18) << left << products[4].name << "RM" << setw(10) << left << products[4].price << "STOCK= " << products[4].stock << endl;
       cout << "6. " << setw(18) << left << products[5].name << "RM" << setw(10) << left << products[5].price << "STOCK= " << products[5].stock << endl;
       cout << "Please enter 1-6 for the product of your choice." << endl;
       cin >> ordervin;

       if ((ordertin < 1) || (ordertin >6))
          {
                cout << "Please enter a number between 1 and 6." << endl;
                system("pause");
                system("cls");
          }
       
       else
       oVerif = true;
      }    
           //have it check/deduct stock after order
           //possible to implement ordering more than 1 item?

      if (ordertin == 1||2||3||4||5||6) OrderVerif();
    
}

//function displaying customer name, order pref, address and items ordered
void OrderFinal ()
{
     int orderfin;
     
     system("cls");
     cout << "what you ordered" << endl;
     cout << "Please check your order details above." << endl;
     cout << "If they are correct, kindly enter 1 to proceed." << endl;
     cout << "Otherwise, enter 2 to re-enter your order." << endl;
     cout << "1. Proceed with order." << endl;
     cout << "2. Re-enter order." << endl;
     cin >> orderfin;
     
     if (orderfin == 1) 
     system("cls");
     cout << "placeholder" << endl;
     if (orderfin == 2)
     system("cls");
     cout << "placeholder" << endl;
}

//Final main function.
int main()
{
    cout << "placeholder" << endl;
    system("pause");
    UserVerif();
    
    
    system ("pause");
    return 0;
}

My main issue is to get the inputs from NameVerif (and the other strings attached to the same name in the vector), PayVerif and OrderVerif to appear in a function at the end.
I also realise that my code is far from optimal or organized, even. I'm trying to improve.
Any feedback and criticism would be welcomed.

Thanks for your reply.
Sorry for being vague, I guess I could say I was looking for generic examples.

Here's my code, although some of it still needs working on.

#include <iostream>
#include <string>
#include <vector>
#include <iomanip>

using namespace std;

//Entering protofunctions first
void UserVerif ();
void UserCreate();
void PayVerif ();
void NameVerif();
void OrderVerif ();
void OrderFinal (); 

struct ExistUserRecord
{
       public:
       string name;
       string address;
       string phone;
       ExistUserRecord(string a, string b, string c)
       {
          name    = a;
          address = b;
          phone   = c;
       }
};

struct NewUserRecord
{
      public:       
      string name;
      string address;
      string phone;
      NewUserRecord(string a, string b, string c)
       {
          name    = a;
          address = b;
          phone   = c;
       }
};

//Product class
struct ProdRecord
{
      public:
      string name;
      float price;
      int stock;
      ProdRecord(string a, float b, int c)
      {
         name  = a; 
         price = b; 
         stock = c;
      }
};

//User account verif. function
void UserVerif ()
{
     int userin;
     bool uVerif = false;
     
     while (uVerif == false)
     {
         cout << "Before placing your order, do you have an existing user account?" << endl;
         cout << "1. Yes" << endl;
         cout << "2. No" << endl;
         cout << "Please enter 1 or 2 for your account status." << endl;
         cin >> userin;
      
         if ((userin < 1) || (userin > 2))
         {
            cout << "Please enter either 1 or 2." << endl;
            system("pause");
            system("cls");
         }
         else
         uVerif = true;
     }
      if (userin == 1)      NameVerif();  //go to account name verif. function
      else if (userin == 2) UserCreate(); //go to account creation function
}


//New user creation function
void UserCreate()
{
     vector<NewUserRecord>users;
     
     string name;
     string address;
     string phone;
    
     NewUserRecord *u1;
     
     system("cls");
     cout << "Please enter your full name." << endl;
     cin >> name;
     cout << "Please enter your address (Only 1 line)." << endl;
     cin >> address;
     cout << "Please enter your phone number." << endl;
     cin >> phone;
     
     users.push_back(NewUserRecord(name, address, phone));
     PayVerif(); //after creating account go to pay verification
}


//account name verif. function
void NameVerif ()
{
     string namein;
     vector<ExistUserRecord>users;
    
     users.push_back(ExistUserRecord("Adam", "14 Alpha Street", "013-4897412"));
     users.push_back(ExistUserRecord("Bob", "18 Beta Avenue", "016-7513547"));
     users.push_back(ExistUserRecord("Carol", "9 Zeta Lane", "019-1355497"));
     
     system("cls");
     cout << "Please enter your full name for us to verify your account." << endl;
     cin >> namein;
     
     if (namein == users[0].name)
     {
     cout << "Welcome back, Adam." << endl;
     system("pause");
     PayVerif();
     }
     if (namein == users[1].name)
     {
     cout << "Welcome back, Bob." << endl;
     system("pause");
     PayVerif();
     }
     if (namein == users[2].name)
     {
     cout << "Welcome back, Carol." << endl;
     system("pause");
     PayVerif();
     }
     else
     {
     int namefailin;
     bool nVerif = false;
     
     while (nVerif == false)
           {
              system("cls");     
              cout << "We could not find your name in the database." << endl;
              cout << "Please enter the following numbers for your next choice." << endl;
              cout << "1. Re-enter your name." << endl;
              cout << "2. Create a new user account." << endl;
              cin >> namefailin;
            
              if ((namefailin < 1) || (namefailin > 2))
                 {
                   cout << "Please enter either 1 or 2." << endl;
                   system("pause");
                   system("cls");
                 }
              else
              nVerif = true;
           }
           if (namefailin == 1)      NameVerif();
           else if (namefailin == 2) UserCreate();
     }

     system("pause");
     PayVerif();
}


//paypal verif. function
void PayVerif()
{
     int payin;
     bool pVerif = false;
     
     while (pVerif == false)
     {
           system("cls");
           cout << "Are you paying via:" << endl;
           cout << "1. Credit card" << endl;
           cout << "2. Paypal" << endl;
           cout << "3. Neither" << endl;
           cout << "Please enter 1,2 or 3 for your payment choice." << endl;
           cin >> payin;
           
           if ((payin < 1) || (payin > 3))
           {
                cout << "Please enter a number between 1 and 3." << endl;
                system("pause");
                system("cls");
           }
           
           else
           pVerif = true;
     }     
           if      (payin == 1) OrderVerif(); //move to order taking function
           else if (payin == 2) OrderVerif(); //move to order taking function
           else if (payin == 3)
           {
                system("cls"); 
                cout << "I'm sorry, but our service only accepts credit cards or Paypal as a payment choice." << endl;    
           }
}


//order taking function
void OrderVerif()
{
     int ordertin;
     bool oVerif = false;
     
     while (oVerif == false)
     {
       vector<ProdRecord>products;
       
       products.push_back(ProdRecord("Diablo 3", 150.00, 0));
       products.push_back(ProdRecord("Call of Duty 5", 150.00, 20));
       products.push_back(ProdRecord("Starcraft 3", 150.00, 30));
       products.push_back(ProdRecord("Battlefield 4", 150.00, 30));
       products.push_back(ProdRecord("Warcraft 4", 150.00, 25));
       products.push_back(ProdRecord("Catz 'N Dogz", 50.00, 100));
     
       system("cls");
       cout << fixed << setprecision(2);
       cout << "Please select your desired product:" << endl;
       cout << "1. " << setw(18) << left << products[0].name << "RM" << setw(10) << left << products[0].price << "STOCK= " << products[0].stock << endl;
       cout << "2. " << setw(18) << left << products[1].name << "RM" << setw(10) << left << products[1].price << "STOCK= " << products[1].stock << endl;
       cout << "3. " << setw(18) << left << products[2].name << "RM" << setw(10) << left << products[2].price << "STOCK= " << products[2].stock << endl;
       cout << "4. " << setw(18) << left << products[3].name << "RM" << setw(10) << left << products[3].price << "STOCK= " << products[3].stock << endl;
       cout << "5. " << setw(18) << left << products[4].name << "RM" << setw(10) << left << products[4].price << "STOCK= " << products[4].stock << endl;
       cout << "6. " << setw(18) << left << products[5].name << "RM" << setw(10) << left << products[5].price << "STOCK= " << products[5].stock << endl;
       cout << "Please enter 1-6 for the product of your choice." << endl;
       cin >> ordervin;

       if ((ordertin < 1) || (ordertin >6))
          {
                cout << "Please enter a number between 1 and 6." << endl;
                system("pause");
                system("cls");
          }
       
       else
       oVerif = true;
      }    
           //have it check/deduct stock after order
           //possible to implement ordering more than 1 item?

      if (ordertin == 1||2||3||4||5||6) OrderVerif();
    
}

//function displaying customer name, order pref, address and items ordered
void OrderFinal ()
{
     int orderfin;
     
     system("cls");
     cout << "what you ordered" << endl;
     cout << "Please check your order details above." << endl;
     cout << "If they are correct, kindly enter 1 to proceed." << endl;
     cout << "Otherwise, enter 2 to re-enter your order." << endl;
     cout << "1. Proceed with order." << endl;
     cout << "2. Re-enter order." << endl;
     cin >> orderfin;
     
     if (orderfin == 1) 
     system("cls");
     cout << "placeholder" << endl;
     if (orderfin == 2)
     system("cls");
     cout << "placeholder" << endl;
}

//Final main function.
int main()
{
    cout << "placeholder" << endl;
    system("pause");
    UserVerif();
    
    
    system ("pause");
    return 0;
}

My main issue is to get the inputs from NameVerif (and the other strings attached to the same name in the vector), PayVerif and OrderVerif to appear in a function at the end.
I also realise that my code is far from optimal or organized, even. I'm trying to improve.
Any feedback and criticism would be welcomed.

Group all your functions, variables, and structs (if the structs are still necessary after the fact) into a single class. That way you'll have access to, and can make changes to, everything you need without having to worry about scope.

I still don't have much experience with classes, so I'll try it in another program.
Anyway, I tried my hand at changing NameVerif and I get a linker error everytime.

getline(cin, input);

Also doesn't seem to be working so I just stuck with

cin >> input;
void NameVerif (&string namein)
{
     vector<ExistUserRecord>users;
    
     users.push_back(ExistUserRecord("Adam", "14 Alpha Street", "013-4897412"));
     users.push_back(ExistUserRecord("Bob", "18 Beta Avenue", "016-7513547"));
     users.push_back(ExistUserRecord("Carol", "9 Zeta Lane", "019-1355497"));
     
     system("cls");
     cout << "Please enter your full name for us to verify your account." << endl;
     cin >> namein;
     
     if (namein == users[0].name)
     {
     cout << "Welcome back, Adam." << endl;
     system("pause");
     PayVerif();
     }
     if (namein == users[1].name)
     {
     cout << "Welcome back, Bob." << endl;
     system("pause");
     PayVerif();
     }
     if (namein == users[2].name)
     {
     cout << "Welcome back, Carol." << endl;
     system("pause");
     PayVerif();
     }
     else
     {
     int namefailin;
     bool nVerif = false;
     
     while (nVerif == false)
           {
              system("cls");     
              cout << "We could not find your name in the database." << endl;
              cout << "Please enter the following numbers for your next choice." << endl;
              cout << "1. Re-enter your name." << endl;
              cout << "2. Create a new user account." << endl;
              cin >> namefailin;
            
              if ((namefailin < 1) || (namefailin > 2))
                 {
                   cout << "Please enter either 1 or 2." << endl;
                   system("pause");
                   system("cls");
                 }
              else
              nVerif = true;
           }
           if (namefailin == 1)      NameVerif();
           else if (namefailin == 2) UserCreate();
     }

     system("pause");
     PayVerif();
}

I still don't have much experience with classes, so I'll try it in another program.
Anyway, I tried my hand at changing NameVerif and I get a linker error everytime.

getline(cin, input);

Also doesn't seem to be working so I just stuck with

cin >> input;
void NameVerif (&string namein)
{
     vector<ExistUserRecord>users;
    
     users.push_back(ExistUserRecord("Adam", "14 Alpha Street", "013-4897412"));
     users.push_back(ExistUserRecord("Bob", "18 Beta Avenue", "016-7513547"));
     users.push_back(ExistUserRecord("Carol", "9 Zeta Lane", "019-1355497"));
     
     system("cls");
     cout << "Please enter your full name for us to verify your account." << endl;
     cin >> namein;
     
     if (namein == users[0].name)
     {
     cout << "Welcome back, Adam." << endl;
     system("pause");
     PayVerif();
     }
     if (namein == users[1].name)
     {
     cout << "Welcome back, Bob." << endl;
     system("pause");
     PayVerif();
     }
     if (namein == users[2].name)
     {
     cout << "Welcome back, Carol." << endl;
     system("pause");
     PayVerif();
     }
     else
     {
     int namefailin;
     bool nVerif = false;
     
     while (nVerif == false)
           {
              system("cls");     
              cout << "We could not find your name in the database." << endl;
              cout << "Please enter the following numbers for your next choice." << endl;
              cout << "1. Re-enter your name." << endl;
              cout << "2. Create a new user account." << endl;
              cin >> namefailin;
            
              if ((namefailin < 1) || (namefailin > 2))
                 {
                   cout << "Please enter either 1 or 2." << endl;
                   system("pause");
                   system("cls");
                 }
              else
              nVerif = true;
           }
           if (namefailin == 1)      NameVerif();
           else if (namefailin == 2) UserCreate();
     }

     system("pause");
     PayVerif();
}

The most critical problem you have here is that your vectors are declared inside your functions--meaning they will destruct as soon as your program is done executing that function, and you can no longer access that information. Reorganize your code such that all your vectors are declared in main, and pass the vector you need into the functions. You also don't want two structs for users. Have a vector of users, and use NameVerify to search that vector for the name entered, and if not found, add to it using push_back. Have NameVerify return the vector location of the user. (if it's a new user, the vector location will be at the end. Then have PayVerif do the same thing, only with products. Have PayVerif search the products vector for the product in question, and return the location of the vector. Then pass the two structs to PrintInvoice.

int NameVerify(vector<UserRecord>& users);
int PayVerify(vector<Products>& products);
void PrintInvoice(struct UserRecord user, struct Products product);


int main(){
vector<UserRecord> Users;
vector<ProductRecord> Products;
//..have your products.push_back here if you like, or in a separate function

int user_vector_location = NameVerify(Users);
int product_vector_location = PayVerify(Products);
PayVerify(Users[user_vector_location], Products[product_vector_location]);
return 0;
}

//sample NameVerify code
int NameVerify(vector<UserRecord>& users){
  string namein;
  cout << "enter name";
  cin >> name;
  for (int i = 0; i < users.size(); ++i){
    if (users[i].name = namein) return i;
  }
  //so a name was already found, return the location in the vector... 
  //the function will not go past this point if a name was found...
  //so if the function continues, it must be a new user... 
  string address;
  string phone;
  cin >> address;
  cin >> phone;
  users.push_back(name, address, phone);
  return users.size() - 1; // returns the location to the last element in the vector
}

Anyway, hope that helps some.

Edited 5 Years Ago by Greywolf333: n/a

This article has been dead for over six months. Start a new discussion instead.