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