Good Afternoon,

The program that I'm doing with function doesn't want to run, the error that pops out is: 'std::basic_istream<_Elem,_Traits> std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,signed char *)'

Here is the code and header file that I have so far:

CPP FILE

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cmath> 
#include <cstdlib>
#include <iomanip>

//include my own header file
#include "lab_14_head.h"

using namespace std; 

int main( )
{

    /***************************************************************
    Part A. Practice
    ***************************************************************/

    //variable declaration
    int number1, number2, result;   //vars to store two int values
    int flag;                       //for option selection

    //call showMenu() to show the menu
    showMenu(); 

    //get option
    cout<<" Enter your choice => "; 
    cin>>flag; 

    //four case selection using switch
    switch(flag)
    {
        case 1:                     //call fun_1()
                cout<<" Calling fun_1() ......................... "<<endl; 
                fun_1(); 
                cout<<" End of calling fun_1()" <<endl;  
                break; 
        case 2:                     //call fun_2()
                cout<<" Calling fun_2() ......................... "<<endl; 
                cout<<" we must pass two parameters to it         "<<endl; 
                //get two numbers
                cout<<"Enter two numbers ==> "; 
                cin>>number1>>number2; 
                //function calling
                fun_2(number1, number2);        //see parameter passing 
                cout<<" End of calling fun_2()" <<endl;  
                break; 
        case 3:                     //call fun_3()
                cout<<" Calling fun_3() ......................... "<<endl; 
                cout<<" We shall receive the value returned       "<<endl;
                result = fun_3();               //see how we recieve the value
                cout<<" The sum is " << result <<endl; 
                cout<<" End of calling fun_3()" <<endl; 
                break; 
        case 4:                     //call fun_4()
                cout<<" Calling fun_4() ......................... "<<endl; 
                cout<<" We must pass two parameters to it         "<<endl; 
                cout<<" We shal also receive the value returned   "<<endl; 
                //get two numbers
                cout<<"Enter two numbers ==> "; 
                cin>>number1>>number2; 
                //function calling
                result = fun_4(number1, number2); //see parameter passing and value receiving
                cout<<" The sum is " << result <<endl; 
                cout<<" End of calling fun_4()" <<endl; 
                break; 
        default:
                cout<<"You entered a wrong choice "<<endl; 
    }

    /*******************************************************************************
     Part B. Exercise. This part is for you to complete.Re-do Part A. However, you
     shall use four functions to get firstName, lastName and print the name. Pay
     close attention to the difference between "void type" and "value-returning type" 
     and the difference between "with parameters" and "without parameters".
     *******************************************************************************/
    string Fname, Lname, fullname;  //vars to store two int values
    int flag;                       //for option selection

    //call showMenu() to show the menu
    showMenu(); 

    //get option
    cout<<" Enter your choice => "; 
    cin>>flag; 

        if(flag == 5)
        {

                cout<<" Calling fun_5() ......................... "<<endl; 
                fun_5(); 
                cout<<" End of calling fun_5()" <<endl;  
        }       
        else if (flag ==6)
        {
                cout<<" Calling fun_6() ......................... "<<endl; 
                cout<<" we must pass two parameters to it         "<<endl; 

                cout<<"Enter your first and last name ==> "; 
                cin>>Fname>>" ">>Lname; 
                //function calling
                fun_6(Fname, Lname);        //see parameter passing 
                cout<<" End of calling fun_6()" <<endl;  
        }       
        else if (flag ==7)
        {
                cout<<" Calling fun_7() ......................... "<<endl; 
                cout<<" We shall receive the value returned       "<<endl;
                fullname = fun_7();             //see how we recieve the value
                cout<<" Your fullname is " << fullname <<endl; 
                cout<<" End of calling fun_7()" <<endl; 
        }   
        else if (flag ==8)
        {
                cout<<" Calling fun_8() ......................... "<<endl; 
                cout<<" We must pass two parameters to it         "<<endl; 
                cout<<" We shal also receive the value returned   "<<endl; 

                cout<<"Enter your first and last name ==> "; 
                cin>>Fname>>Lname; 
                //function calling
                fullname = fun_8(Fname, Lname); //see parameter passing and value receiving
                cout<<" Your fullname is " << fullname <<endl; 
                cout<<" End of calling fun_4()" <<endl; 
        }       
        else
                cout<<"You entered a wrong choice "<<endl; 

    //well done and exit
    return 0; 
}

Header File

#include <iostream>

using namespace std; 

#ifndef LAB_14_HEAD_H       
#define LAB_14_HEAD_H

//This function shows the user menu
void showMenu()
{
    cout<<endl; 
        cout<<"********User Menu***************************************************"<<endl; 
        cout<<"Enter 1 to play with a void function without parameters              "<<endl;
        cout<<"Enter 2 to play with a void function parameters                        "<<endl;
        cout<<"Enter 3 to play with a value-returning function without parameters   "<<endl;
        cout<<"Enter 4 to play with a value-returning function with   parameters    "<<endl;
        cout<<"********User Fullname***********************************************"<<endl;
        cout<<"Enter 5 to play with a void function without parameters              "<<endl;
        cout<<"Enter 6 to play with a void function parameters                        "<<endl;
        cout<<"Enter 7 to play with a value-returning function without parameters   "<<endl;
        cout<<"Enter 8 to play with a value-returning function with   parameters    "<<endl;
    return;         //no value is returned here
}

//this is a void function without parameters
//it gets two numbers from the user and prints the sum
void fun_1( )
{
    //local variable declaration
    int x, y, sum; 

    //get two numbers
    cout<<"Enter two numbers ==> "; 
    cin>>x>>y; 

    //find the sum
    sum=x+y; 

    //show the sum
    cout<<x<<"  +  " <<y<<"  =  " <<sum<<endl; 

    //stop
    return;         //no value is returned here
}

//this is a void function with parameters
//it gets two numbers from the user and prints the sum
void fun_2(int x, int y)//x and y are two parameters
{
    //local variable declaration
    int sum; 

    //find the sum
    sum=x+y; 

    //show the sum
    cout<<x<<"  +  " <<y<<"  =  " <<sum<<endl; 

    //stop
    return;         //no value is returned here
}

//this is a value returning function without parameters
//it gets two numbers from the user and returns the sum
int fun_3( )        //x and y are two parameters
{
    //local variable declaration
    int x, y, sum; 

    //get two numbers
    cout<<"Enter two numbers ==> "; 
    cin>>x>>y; 

    //find the sum
    sum=x+y; 

    //return sum
    return sum;      //sum is returned here
}

//this is a value-returning function with parameters
//it gets two numbers from the user and returns the sum
int fun_4(int x, int y)//x and y are two parameters
{
    //local variable declaration
    int sum; 

    //find the sum
    sum=x+y; 

    //return sum
    return sum;      //sum is returned here
}

//this is a void function without parameters
//it gets the First and Last name from the user and prints the full name
void fun_5( )
{
    //local variable declaration
    string Fname, Lname, fullname; 

    //get the First and Last name
    cout<<"Enter your first and last name ==> "; 
    cin>>Fname>>" " >>Lname;

    //get the full name
    fullname=Fname + Lname; 

    //show the full name
    cout<<Fname<<"  +  " <<Lname<<"  =  " <<fullname<<endl; 

    //stop
    return;         //no value is returned here
}

//this is a void function with parameters
//it gets the first and last name from the user and prints the fullname
void fun_6(string Fname, string Lname)
{
    //local variable declaration
    string fullname; 

    //find the fullname
    fullname=Fname + Lname; 

    //show the fullname
    cout<<Fname<<"  +  " <<Lname<<"  =  " <<fullname<<endl; 

    //stop
    return;         //no value is returned here
}

//this is a value returning function without parameters
//it gets the first and last name from the user and returns the fullname
string fun_7( )     
{
    //local variable declaration
    string Fname, Lname, fullname; 

    //get the first and last name
    cout<<"Enter your first and last name ==> "; 
    cin>>Fname>>" " >>Lname; 

    //find the fullname
    fullname = Fname + Lname; 

    //return sum
    return fullname;         
}
//this is a value-returning function with parameters
//it gets the first and last name from the user and returns the fullname
string fun_8(string Fname, string Lname)
{
    //local variable declaration
    string fullname; 

    //find the fullname
    fullname = Fname + Lname; 

    //return the fullname
    return fullname;        
}
#endif

OK, first things first: I know that you were probably instructed to put the functions in the header file, but in fact you only want the function prototypes declared there; you'll want to put the actual functions in a third file, which would then be linked into the program at compile time.

#ifndef LAB_14_HEAD_H
#define LAB_14_HEAD_H
//This function shows the user menu

#include <string>

void showMenu();
void fun_1();
void fun_2(int x, int y);
int fun_3( );
int fun_4(int x, int y);
void fun_5();
void fun_6(std::string Fname, std::string Lname);
std::string fun_7();
std::string fun_8(std::string Fname, std::string Lname);

#endif

Just how you would do this depends on your working environment and how you are compiling it; which is to say, it works differently for Visual Studio than how it works in Code::Blocks, which is different from how you would do it from the command line, etc.

As for the reasons why you don't want the functions themselves in the header files, this posting gives a thorough, if tongue-in-cheek, explanation for it as well as some historical context.

Now, to address the specific error message in question, let me ask you, does it say what line the error is occurring on? I suspect that it is a namespace issue, but without more information I cannot say for certain.

Edited 4 Years Ago by Schol-R-LEA

Having taken the time to compile the code and run it, I've found that the error in question occurs on line
104 (and repeated on line 142), where you have an invalid cin.operator>>() reference:

cin>>Fname>>" " >>Lname;

You cannot have any literals inside a cin>> operation. In this case, the space is unnecessary, anyway, as cin >> automagically breaks the input at whitespace. Thus, this is all you should need:

std::cin >> Fname >> LName;

Note that used the fully qualified version of the std::cin object; I recommend doing this most of the time, rather than using the using namespace std; directive. A fully working version of your code using explicit scoping would be:

#include <string>
#include <iostream>
#include "lab_14_head.h"


void showMenu()
{
    std::cout<<std::endl;
    std::cout<<"********User Menu***************************************************"<<std::endl;
    std::cout<<"Enter 1 to play with a void function without parameters "<<std::endl;
    std::cout<<"Enter 2 to play with a void function parameters "<<std::endl;
    std::cout<<"Enter 3 to play with a value-returning function without parameters "<<std::endl;
    std::cout<<"Enter 4 to play with a value-returning function with parameters "<<std::endl;
    std::cout<<"********User Fullname***********************************************"<<std::endl;
    std::cout<<"Enter 5 to play with a void function without parameters "<<std::endl;
    std::cout<<"Enter 6 to play with a void function parameters "<<std::endl;
    std::cout<<"Enter 7 to play with a value-returning function without parameters "<<std::endl;
    std::cout<<"Enter 8 to play with a value-returning function with parameters "<<std::endl;
    return; //no value is returned here
}
//this is a void function without parameters
//it gets two numbers from the user and prints the sum
void fun_1( )
{
    //local variable declaration
    int x, y, sum;
    //get two numbers
    std::cout<<"Enter two numbers ==> ";
    std::cin>>x>>y;
    //find the sum
    sum=x+y;
    //show the sum
    std::cout<<x<<" + " <<y<<" = " <<sum<<std::endl;
    //stop
    return; //no value is returned here
}
//this is a void function with parameters
//it gets two numbers from the user and prints the sum
void fun_2(int x, int y)//x and y are two parameters
{
    //local variable declaration
    int sum;
    //find the sum
    sum=x+y;
    //show the sum
    std::cout<<x<<" + " <<y<<" = " <<sum<<std::endl;
    //stop
    return; //no value is returned here
}
//this is a value returning function without parameters
//it gets two numbers from the user and returns the sum
int fun_3( ) //x and y are two parameters
{
    //local variable declaration
    int x, y, sum;
    //get two numbers
    std::cout<<"Enter two numbers ==> ";
    std::cin>>x>>y;
    //find the sum
    sum=x+y;
    //return sum
    return sum; //sum is returned here
}
//this is a value-returning function with parameters
//it gets two numbers from the user and returns the sum
int fun_4(int x, int y)//x and y are two parameters
{
    //local variable declaration
    int sum;
    //find the sum
    sum=x+y;
    //return sum
    return sum; //sum is returned here
}
//this is a void function without parameters
//it gets the First and Last name from the user and prints the full name
void fun_5( )
{
    //local variable declaration
    std::string Fname, Lname, fullname;
    //get the First and Last name
    std::cout<<"Enter your first and last name ==> ";
    std::cin >> Fname >> Lname;
    //get the full name
    fullname=Fname + Lname;
    //show the full name
    std::cout<<Fname<<" + " <<Lname<<" = " <<fullname<<std::endl;
    //stop
    return; //no value is returned here
}
//this is a void function with parameters
//it gets the first and last name from the user and prints the fullname
void fun_6(std::string Fname, std::string Lname)
{
    //local variable declaration
    std::string fullname;
    //find the fullname
    fullname=Fname + Lname;
    //show the fullname
    std::cout<<Fname<<" + " <<Lname<<" = " <<fullname<<std::endl;
    //stop
    return; //no value is returned here
}
//this is a value returning function without parameters
//it gets the first and last name from the user and returns the fullname
std::string fun_7( )
{
    //local variable declaration
    std::string Fname, Lname, fullname;
    //get the first and last name
    std::cout<<"Enter your first and last name ==> ";
    std::cin>>Fname >>Lname;
    //find the fullname
    fullname = Fname + Lname;
    //return sum
    return fullname;
}
//this is a value-returning function with parameters
//it gets the first and last name from the user and returns the fullname
std::string fun_8(std::string Fname, std::string Lname)
{
    //local variable declaration
    std::string fullname;
    //find the fullname
    fullname = Fname + Lname;
    //return the fullname
    return fullname;
}

Note that there may still be more bugs I didn't see; all I fixed were the ones I mentioned earlier.

Edited 4 Years Ago by Schol-R-LEA

Schol -R -Lea,

I already try your reccomendation, but still the program doesn't want to run. I'm still getting the same error.

In that case, could you post the exact error message as it appears in the compiler window, including the line numbers? If you could let us know which compiler and IDE you are using, it may help as well.

Schol-R_ Lea,

I already fix the problems, it was missing a character type in middle of a function. Thank you very much for your help, I really appreciated.

This question has already been answered. Start a new discussion instead.