Visual Studio 2013 programming "<<" is not matching operands? - c++

I am getting an error saying the operand "<<" (right before times3(x) in the main function ) does not match the operand types being outputted in that line. What am I doing wrong? I searched for errors similar to it and found that its an inclusion error but i thought having would fix it. Also, countdown(seconds) in the main function is not being recognized and giving me an error. Why is that? The problems keep occurring when working with void.
'
#include <iostream>
#include <string>
#include <cstdlib>
#include <limits>
using namespace std;
bool die(const string & msg);
double triple(double x);
double times9(double x);
void triple(double & result, double x);
void times3(double & x);
void countdown(unsigned seconds);
bool restore();
int main(){
double x;
cout << "x: " << endl;
cin >> x;
cout << "The triple of " << x << " is " << triple(x) << endl;
cout << "9 times of " << x << " is " << times9(x) << endl;
cout << "3 times of " << x << " is " << times3(x) << endl;
unsigned seconds;
cout << "seconds: " << endl;
cin >> seconds;
cout << countdown(seconds) << endl;
}
bool die(const string & msg){
cout << "Fatal error: " << msg << endl;
exit(EXIT_FAILURE);
}
double triple(double x){
return 3 * x;
}
double times9(double x){
return 3 * triple(x);
}
void triple(double & result, double x){
x = 3 * x;
}
void times3(double & x){
x = triple(x);
}
void countdown(unsigned & seconds){
unsigned count = seconds;
cin >> seconds || die("input failure");
for (unsigned i = seconds; i <= size; i--){
cout << i << endl;
}
cout << "Blast off! " << endl;
}
bool resotre(){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return cin.good();
}'

As mentioned in earlier answer, you need to change the return type of your function from void to the data type of variable your trying to print.
Another issue in your code is with function void countdown(unsigned & seconds)
Declaration and definition of the functions are different.
You have declared it as void countdown(unsigned seconds); but at the time of defining it you are using void countdown(unsigned & seconds). In declaration you are declaring it to take arguments by value but in definition you are making it to take arguments by reference.
Also in the for loop of the function countdown you have written
for (unsigned i = seconds; i <= 0; i--), this won't print any output, since your condition is i<=0, i think you tried to type i >= 0. :)

times3 returns void. Try:
times3(x);
cout << "3 times of " << x << " is " << x << endl;
Or have times3() return double instead of passing by reference.
double times3(double x);

Related

Can't Include function definitions into another source file [duplicate]

This question already has answers here:
duplicate symbol error C++
(4 answers)
How do I use extern to share variables between source files?
(19 answers)
Closed 2 years ago.
Good Evening Everyone,
I'm taking a c++ class right now, and the teacher wants me to include the function definitions into another main. Every time that I do however, I get this error:
Severity Code Description Project File Line Suppression State
Error LNK2005 "unsigned int speed" (?speed##3IA) already defined in Sourcjhkhje.obj Project4 C:\Users\muhammad\source\repos\Project4\Project4\TestBicycle.obj 1
Severity Code Description Project File Line Suppression State
Error LNK1169 one or more multiply defined symbols found Project4 C:\Users\muhammad\source\repos\Project4\Debug\Project4.exe 1
All I need to do is move the function definitions into one source, and the main into another. Anyone know what's causing this issue?
These are what my files look like.
header 1:
#pragma once
size_t speed(0);
size_t GetSpeed();
const size_t MINspeed(10);
const size_t MAXspeed(40);
header 2:
#pragma once
#include <iostream>
#include <iomanip>
using namespace std;
header 3:
#include "BicycleLibIncludes.h"
void SetSpeed(size_t sp);
void DefaultSetSpeed(size_t s);
void DistanceTravelled(size_t x);
size_t GetSpeed();
size_t GetMinSpeed();
size_t GetMaxSpeed();
void GetSelectedSpeed();
source 1:
#include "BicycleLibIncludes.h"
#include "BicyclePrototypes.h"
/*void SetSpeed(size_t sp)
{
//cout << "Enter the current speed:";
//cin >> sp;
if ((sp >= 10) && (sp <= 40))
{
speed = sp;
}
else
{
cout << "This value is too high/low!";
}
}*/
size_t GetSpeed()
{
return (speed);
}
size_t GetMinSpeed()
{
return size_t(MINspeed);
}
size_t GetMaxSpeed()
{
return size_t(MAXspeed);
}
void DefaultSetSpeed(size_t s = 20)
{
speed = s;
}
void GetSelectedSpeed()
{
string speedunit;
while (true)
{
cout << "Enter M or K for mph or kmh respecitvely. Hit anything else to quit.";
cin >> speedunit;
if (speedunit == "M" || speedunit == "K")
{
break;
}
else
{
continue;
}
}
if (speedunit == "M")
{
cout << speed;
}
if (speedunit == "K")
{
double toKmPerHour = 1.61;
double speedinKmPerHour = speed * toKmPerHour;
cout << speedinKmPerHour << "\n";
std::cout << setprecision(3) << speedinKmPerHour << "\n";
cout << static_cast<int>(speedinKmPerHour) << "\n";
cout << static_cast<int>(speedinKmPerHour + 0.5) << "\n";
cout << floor(speedinKmPerHour) << "\n";
cout << ceil(speedinKmPerHour) << "\n";
}
}
void DistanceTravelled(size_t x)
{
static size_t accessCounter;
static int s;
cout << "Total distance for all trips so far: " << s << "\n";
s += x;
cout << "distance travelled for this trip was:" << x;
cout << "\nTotal distance for all trips so far: " << s << "\n";
s += x;
accessCounter++;
cout << "Number of times you've used this program:" << accessCounter;
}
int main()
{
DefaultSetSpeed();
cout << GetSpeed() << "\n";
DefaultSetSpeed(28);
cout << GetSpeed() << "\n";
cout << "Enter the speed you want to set:";
size_t userinput;
cin >> userinput;
SetSpeed(userinput);
cout << "Current Speed = " << GetSpeed()
<< "\nMAXspeed = " << GetMaxSpeed()
<< "\nMINspeed = " << GetMinSpeed() << "\n";
system("pause");
system("cls");
GetSelectedSpeed();
cout << "First trip:\n";
DistanceTravelled(5);
cout << "\nSecond trip:\n";
DistanceTravelled(10);
cout << "\nThird trip:\n:";
DistanceTravelled(15);
}
source 2: (Just did this one as a test)
#include "BicycleLibIncludes.h"
#include "BicyclePrototypes.h"
void SetSpeed(size_t sp)
{
//cout << "Enter the current speed:";
//cin >> sp;
if ((sp >= 10) && (sp <= 40))
{
speed = sp;
}
else
{
cout << "This value is too high/low!";
}
}
Thank you.

Value-Returning Function Debug Error - C++

I am in a beginner C++ course. Here are the instructions for my assignment:
Write a program that calls a value-returning function that prompts the user to enter the weight of a person in pounds and then calls another value returning function to calculate the equivalent weight in kilograms. Output both the weights rounded to two decimal places. (Note that 1 kilogram = 2.2 pounds.) Format your output with two decimal places.
I thought everything was perfect. However, I am a getting a debug error, which is Runtime Check Error #3 - T. Please review my code and tell me what is wrong here. Remember, I am a beginner. Thank you.
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
string get_date();
void student_heading();
float get_user_input(float);
float convert_weight(float);
int main()
{
cout << fixed << showpoint << setprecision(2);
string mydate;
float weight_lbs;
float weight_kgs;
mydate = get_date();
student_heading();
weight_lbs = get_user_input();
weight_kgs = convert_weight(weight_lbs);
return 0;
}
string get_date()
{
string mydate;
cout << "Enter today's date:";
getline(cin, mydate);
return mydate;
}
void student_heading()
{
cout << "*******************" << endl;
cout << "Student" << endl;
cout << "ID Number" << endl;
cout << "SYCS-135" << endl;
cout << "Assignment 6" << endl;
cout << "October 6, 2015" << endl;
cout << "******************" << endl;
}
float get_user_input(float lb_weight)
{
cout << "Please provide us with your weight, in pounds:";
cin >> lb_weight;
return lb_weight;
}
float convert_weight(float kg_weight)
{
float lb_weight;
kg_weight = lb_weight / 2.2;
return kg_weight;
}
Your error is that you are calling weight_lbs = get_user_input();
You cant call get_user_input with no argument because you do not have any function like that.
Your code has several issues.
Your function prototypes are not correct. The parameter in the braces have to contain the variable name as well as data type. So only function(int) is not enaugh, it should be function(int MyVariable). So the error was caused by calling function, which didn't exist, because your function prototype was not correct (expected a parameter).
Your get_user_input() function has a parameter which is not needed. The whole function should look like this:
float get_user_input()
{
cout << "Please provide us with your weight, in pounds:";
float lb_weight;
cin >> lb_weight;
return lb_weight;
}
Function for mass conversion can look like this:
float convert_weight(float lb_weight)
{
return lb_weight / 2.2;
}
Your main function then should look like this:
int main()
{
cout << fixed << showpoint << setprecision(2);
string mydate;
float weight_lbs;
float weight_kgs;
mydate = get_date();
cout << mydate << endl;
student_heading();
weight_lbs = get_user_input();
weight_kgs = convert_weight(weight_lbs);
cout << weight_kgs << "Kg";
getchar();
getchar();
return 0;
}
Don't forget to change the function prototypes to:
float get_user_input();
float convert_weight(float lb_weight);

Dynamic Allocation. No idea what these errors mean

This is a project in which I had to dynamically create an array of structs. No idea what these errors mean or what is wrong with my code.
Based on the advice given so far here most of my problems have been solved. Here is the short list of remaining errors.
/tmp/ccdjbURO.o: In function `main':
assignment8.cpp:(.text+0x5a): undefined reference to `getData(menuItemType&, int&, std::basic_ifstream<char, std::char_traits<char> >&)'
assignment8.cpp:(.text+0x116): undefined reference to `showMenu(menuItemType, int)'
assignment8.cpp:(.text+0x1a5): undefined reference to `showMenu(menuItemType, int)'
assignment8.cpp:(.text+0x29f): undefined reference to `makeSelection(int&, int, int)'
assignment8.cpp:(.text+0x2eb): undefined reference to `printCheck(menuItemType, int, int)'
collect2: error: ld returned 1 exit status
Here are my function prototypes and definitions as requested. I see no difference in the function signatures in the prototypes and definition headings vs the formatting of any function calls in the body of my program.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
struct menuItemType
{
string menuItem;
double menuPrice;
};
const double TAX = 0.05;
const string FILE_NAME = "Ch9_Ex5Data.txt";
int getData(menuItemType&, int&, ifstream);
int makeSelection(int&, int, int);
void showMenu(menuItemType, int);
void printCheck(menuItemType, int, int);
void getInt(int&);
void getChar(char&);
//********************************************************************************
//* getData
//********************************************************************************
int getData(menuItemType* &menuList, int& listSize, ifstream& inFile)
{
inFile.open("Ch9_Ex5Data.txt");
inFile >> listSize;
if (inFile.fail())
return -1;
menuList = new menuItemType[listSize];
for(int i = 0; i < listSize; i++)
{
getline(inFile, menuList[i].menuItem);
inFile >> menuList[i].menuPrice;
if (inFile.fail())
return -1;
break;
}
122,1 47%
return 1;
}
//********************************************************************************
//* makeSelection
//********************************************************************************
int makeSelection(int* &orderList, int quantity, int index)
{
if ((orderList[index] + quantity) < 0)
{
cout << "Quantity selected makes total number ordered less than 0"
<< endl << endl;
return 1;
}
else
{
orderList[index] = orderList[index] + 1;
return -1;
}
}
//********************************************************************************
//* showMenu
//********************************************************************************
void showMenu(menuItemType *menuList, int listSize)
{
cout << fixed << showpoint << setprecision(2) << endl << endl
<< "------Today's Menu------" << endl;
for(int i = 0; i < listSize; i++)
{
cout << left << setw(18) << menuList[i].menuItem << "$ "
<< right << setw(4) << menuList[i].menuPrice << endl;
}
cout << "------------------------"
<< endl << endl;
}
//********************************************************************************
//* printCheck
//********************************************************************************
void printCheck(menuItemType *menuList, int *orderList, int listSize)
{
int taxDue = 0;
int amntDue = 0;
cout << fixed << showpoint << setprecision(2) << endl << endl
<< "------Your Reciept------" << endl;
for (int i = 0; i < listSize; i++)
{
if (orderList[i] > 0)
{
cout << left << setw(2) << orderList[i] << " "
<< setw(15) << menuList[i].menuItem
<< right << setw(5) << (orderList[i] * menuList[i].menuPrice)
<< endl;
amntDue += (orderList[i] * menuList[i].menuPrice);
}
}
210,1 73%
taxDue = amntDue * TAX;
amntDue = amntDue * (1 + TAX);
cout << endl << right << setw(17) << "Tax: $ "
<< setw(7) << taxDue
<< endl << right << setw(17) << "Amount Due: $ "
<< setw(7) << amntDue
<< endl
<< "------------------------" << endl << endl;
}
187,0-1 64%
The errors do look unfriendly, but the issues are simple.
You pass an ifstream in
int getData(menuItemType* &menuList, int& listSize, ifstream inFile)
by value, which implicitly tries to make copy of it.
I think its not possible to copy istream and ostream. You have to pass them by reference.
Also, as #PeterT said in comment, there is no version of getline you are tryng to invoke. Both default versions expect input and string as arguments.
Your forward declarations ARE different from the definitions. Replace them with something like this:
int getData(menuItemType*&, int&, ifstream&);
int makeSelection(int*&, int, int);
void showMenu(menuItemType*, int);
void printCheck(menuItemType*, int*, int);
void getInt(int&);
void getChar(char&);

Classes(Odometer)- Error Message

I was wondering if anyone could give me a clue as to what these error messages mean when I try to compile my code.
Here is the error I get:
in function 'int main()':
not match for 'operator<<'in 'std::operator<<[with_Traits = std::char_traits(((std::basic_ostr...
and it repeats for a while.
I want to post my full code just so you have a idea of what my assignment is, it not that long! =)
#include <iostream>
#include <cstdlib>
using namespace std;
class Odometer
{
public:
Odometer();
void reset();
void totalfuel();
void input_miles(int getmiles);
void Odometer::set_fuel_efficiency(double fuel_efficiency);
int gallonsUsed;
private:
int milesDriven;
double fuel_efficiency;
int getmiles;
};
Odometer::Odometer()
{
milesDriven = 0;
fuel_efficiency = 0;
}
void Odometer::reset()
{
milesDriven = 0;
}
void Odometer::totalfuel()
{
fuel_efficiency = (milesDriven/gallonsUsed);
}
void Odometer::input_miles(int miles_driven)
{
milesDriven = milesDriven + miles_driven;
}
void Odometer::set_fuel_efficiency(double Fuel_efficiency)
{
fuel_efficiency = Fuel_efficiency;
}
double Odometer::getgallons()
{
return milesDriven/fuel_efficiency;
}
// ======================
// main function
// ======================
int main()
{
// Two test trips
Odometer trip1, trip2;
trip1.reset();
trip1.set_fuel_efficiency(45);
trip1.input_miles(100);
cout << "For your fuel-efficient small car:" << endl;
cout << "After 100 miles, " << trip1.totalfuel() << " gallons used." << endl;
trip1.input_miles(50);
cout << "After another 50 miles, " << trip1.totalfuel() << " gallons used." << endl;
trip2.reset();
trip2.set_fuel_efficiency(13);
trip2.input_miles(100);
cout << "For your gas guzzler:" << endl;
cout << "After 100 miles, " << trip2.totalfuel() << " gallons used." << endl;
trip2.input_miles(50);
cout << "After another 50 miles, " << trip2.totalfuel() << " gallons used." << endl;
system("PAUSE");
return 0;
}
What would you expect cout << void to print?
totalfuel() returns void, and you're passing it as a parameter to cout::operator <<. Did you mean to return something from the method?
Perhaps:
double Odometer::totalfuel()
{
fuel_efficiency = (milesDriven/gallonsUsed);
return fuel_efficiency;
}
totalFuel() returns void. I think you meant to invoke the getgallons() method instead.

"overloaded member function not found" reference

I have a problem with my code. I tried to use reference to take my variables from method to main() function and i get this error : "overloaded member function not found". Please Help! :)
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
class Tworzenie_postaci {
public:
Tworzenie_postaci();
string Nazwa_Postaci();
int Wiek_Postaci();
int Staty_Postaci(int Final_Postac_Sila, int Final_Postac_Inteligencja);
int Staty_Postaci();
private:
};
Tworzenie_postaci::Tworzenie_postaci() {
}
string Tworzenie_postaci::Nazwa_Postaci()
{
string wpisz_nazwa;
cout << "Wybierz imie dla swojej postaci:\n";
cin >> wpisz_nazwa;
cout << "Nazwa twojej postaci to:\n" << wpisz_nazwa<< "\n";
return wpisz_nazwa;
}
int Tworzenie_postaci::Wiek_Postaci()
{
int wiek;
cout << "Ile twoja postac ma lat?\n";
cin >> wiek;
cout << "Wiec twoja postac ma " << wiek << " lat\n";
return wiek;
}
void Tworzenie_postaci::Staty_Postaci(int& _Postac_Sila, int& _Postac_Inteligencja) {
int Postac_Sila;
int Postac_Inteligencja;
cout << "Ile twoja postac ma sily? :\n";
cin >> Postac_Sila;
cout << "Twoja postac ma " << Postac_Sila << " sily \n";
cout << "Ile twoja postac ma inteligencji? : \n";
cin >> Postac_Inteligencja;
cout << "Twoja postac ma "<< Postac_Inteligencja << " inteligencji \n";
_Postac_Sila = Postac_Sila;
_Postac_Inteligencja = Postac_Inteligencja;
}
int main()
{
Tworzenie_postaci Postac;
string Final_Imie;
int Final_Wiek;
int Final_Postac_Sila;
int Final_Postac_Inteligencja;
Final_Imie = Postac.Nazwa_Postaci();
Final_Wiek = Postac.Wiek_Postaci();
Postac.Staty_Postaci(Final_Postac_Sila, Final_Postac_Inteligencja);
cout << "\n " << Final_Postac_Sila;
return 0;
}
Sorry for polish names of variables or functions but it is easier for me that way :P
The function is declared as:
int Staty_Postaci(int Final_Postac_Sila,
int Final_Postac_Inteligencja);
It is defined as:
void Tworzenie_postaci::Staty_Postaci(int& _Postac_Sila,
int& _Postac_Inteligencja)
{
...
}
As you can see, the types in the declaration and the definition don't match.
Change one of them to match the other.
Also, the return types must match.