I've been having troubles in getting 2 variables from a function. I've tried to put 2 variables in a function and somewhat code it like in vb put it doesn't save it, even if i declare 2 variable as globals outside of the main. and also my loop statement is kind of not ending if im put in a letter instead of a number. anyone who can help me with this? this is my code.. Thanks

#include <iostream.h>
#include <stdlib.h>
#include <conio.h>

char cname;
char cadd;
void displaymenu();
int getselection();
char getacct(char, char);
void showacct(char, char);

int main(){
    char sel;

    while(sel!=3){

        clrscr();
        displaymenu();
        sel = getselection();

        switch(sel){

            case 1:
                clrscr();
                getacct(cname,cadd);
                getch();
                break;
            case 2:
                clrscr();
                showacct(cname,cadd);
                getch();
                break;

        }


    }

    clrscr();
    cout << "Goodbye!!!";
    getch();
    return 0;
}

void displaymenu(){
    cout << "\n\nAyn Interactive";
    cout << "\nBanking Account Information";
    cout << "\n 1. Create an account";
    cout << "\n 2. View an account";
    cout << "\n 3. Exit Program";
}

int getselection(){
    int s;
    cout << "\nSelection: ";
    cin >> s;
    return s;
}

char getacct(char cn,char ca){
    cout << "Please enter name: ";
    cin >> cn;
    cout << "Please enter address: ";
    cin >> ca;

    return cn,ca;

}

void showacct(char cn,char ca){
    cout << "Name is: " << cn;
    cout << "Address is: " << ca;
}

Recommended Answers

All 17 Replies

return cn,ca; You can only return one value at a time, there is a way around this though, it is called passing values by reference.

To pass by reference, you simply add & after the data types in the parameter list in both the function header and function prototype. This makes it so that whatever variable handed to the function is changed in the function, for example:

void getacct(char & cn,char & ca){
    cout << "Please enter name: ";
    cin >> cn;
    cout << "Please enter address: ";
    cin >> ca;
}

getacct(var1, var2); // var1 and var2 are passed by reference, so their values values are modified, and thus there is no need to return a value.

Also note that you are using the char data type which can only hold one character, not a whole name or address, consider using a string (requires #include <string>) and getline(cin, stringname); instead

An error has shown when i changed the function prototype to string. Style of function definition is obsolete.

#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>

char cname;
char cadd;
void displaymenu();
int getselection();
char getacct(string, string);
void showacct(string, string);

Here is the code now..

#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>
#include <string.h>

void displaymenu();
int getselection();
char getacct(string, string);
void showacct(string, string);

int main(){
    char sel;
    string cname;
    string cadd;


    while(sel!=3){

        clrscr();
        displaymenu();
        sel = getselection();

        switch(sel){

            case 1:
                clrscr();
                getacct(cname,cadd);
                getch();
                break;
            case 2:
                clrscr();
                showacct(cname,cadd);
                getch();
                break;

        }


    }

    clrscr();
    cout << "By: Ian Monong";
    getch();
    return 0;
}

void displaymenu(){
    cout << "\n\nAyn Interactive 2008";
    cout << "\nBanking Account Information";
    cout << "\n 1. Create an account";
    cout << "\n 2. View an account";
    cout << "\n 3. Exit Program";
}

int getselection(){
    int s;
    cout << "\nSelection: ";
    cin >> s;
    return s;
}

char getacct(string & cn,string & ca){
    cout << "Please enter name: ";
    getline(cin, cn);
    cout << "Please enter address: ";
    getline(cin, ca)
}

void showacct(string cn,String ca){
    cout << "Name is: " << cn;
    cout << "Address is: " << ca;
}

I also change the prototype of getacct to void and got rid of the return and put the global variables within the main function but still has that error "obsolete definition"..

Here I added corrections and comments on concepts you should understand. (although I didn't comment on typos and common syntax errors like forgetting a semicolon or accidentally capitalizing the first letter of something when you shouldn't have)

#include <iostream> //.h is not standard, <iostream> and <iostream.h> are 2 different things
#include <stdlib.h>
#include <conio.h>
#include <dos.h>
#include <string> //notice, no .h

using std::string; // the class declaration for string is defined in the std namespace
using std::cout; // for the now standard <iostream> you need to have access to cout declared in the std namespace
using std::cin; // read the above statement
// although you can just type "using namespace std;" to replace these statements, it is
// generally considered bad programming to do so since you are adding declarations you aren't using
// that might contradict other declarations you might have included into the program.

void displaymenu();
int getselection();
void getacct(string &, string &); // you still had the prototype returning char, also need to add & for reference
void showacct(string, string);

int main(){
char sel;
string cname;
string cadd;


while(sel!=3){

system("cls"); // to clear the screen on dos, just send the system the cls message
displaymenu();
sel = getselection();

switch(sel){

case 1:
system("cls");
getacct(cname,cadd);
getch();
break;
case 2:
system("cls");
showacct(cname,cadd);
getch();
break;

}


}

system("cls");
cout << "By: Ian Monong";
getch();
return 0;
}

void displaymenu(){
cout << "\n\nAyn Interactive 2008";
cout << "\nBanking Account Information";
cout << "\n 1. Create an account";
cout << "\n 2. View an account";
cout << "\n 3. Exit Program";
}

int getselection(){
int s;
cout << "\nSelection: ";
cin >> s;
cin.ignore(); // if you use both getline and cin >> in a program, always call cin.ignore()
// after using cin to ignore the next character in the buffer, and cin leaves the newline
// character in the buffer
return s;
}

void getacct(string & cn,string & ca){
cout << "Please enter name: ";
getline(cin, cn);
cout << "Please enter address: ";
getline(cin, ca);
}

void showacct(string cn,string ca){
cout << "Name is: " << cn;
cout << "Address is: " << ca;
}

If you need more help, I will check back on this thread.

I recommend you read the narue's sticky thread about clearing the input buffer, it gives some insight about using "standard" input functions.

Iostream is not working.. Im using a Borlan Turbo C++ version 3.. What's the latest version? i think whats causing the error is the compiler..

That I believe was the first compiler I tried when I started learning C++, but after having a butt load of errors building even the simplest hello world program, I learned it didn't conform to the new standards.

Since I haven't used it since, I can't help you convert the code to work there, I believe one of the best combined IDE/Compilers is Dev-C++ that uses the mingw compiler, which complies with current standards.

that explains it..:) can to give me a link where i can download dev c++?

I feel like a stinker for promoting my own fave IDE, but here lol, http://www.bloodshed.net/dev/devcpp.html , get the "Dev-C++ 5.0 beta 9.2 (4.9.9.2) (9.0 MB) with Mingw/GCC 3.4.2" one. Let me know if you get your program working with this :D

Whats the difference of having a bloodsheed and a normal dev c++?

bloodshed is just the name of the group that made dev-c++.

Ohh.. is this the new standard right now? anyways thanks.. i'll still figure out why its looping like hell when i put a character..

dev c++ is not the new standard, it just conforms to the standard unlike turbo.

as for why your thing is looping like hell, read about what i had to say (both my posts) in this thread.

http://www.daniweb.com/forums/thread112589.html

you will see that if it can't stash the char in the variable type you want to in, it will just keep TRYING to read it without success.

I see.. So if the value being saved for the variable is not of the same format as the one i'm having a condition with it won't recognize it? so i really need to have another function or condition that compares it with the set of chars im using for the selection?

Since you are only reading in a single character, be it a number or non-number character, to avoid the confusion of actually implementing a solution like this, just read into a char variable, and then validate that character, there will be no more infinite looping.

what do you mean? btw i changed the selection values to letters instead of numbers and change the variable type of function and prototype to char and it works well.. Thanks for all the help.. I really appreciate it.. I'll try also using Cfree i heard its like dev-C++ also but not for free..

and also can dev-C++ do the inportb or inport and outportb and outport?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.