### The solution to activity 2 of hour 5 in SamsTeachYourself C++?

Hey, I've been going through Sams Teach Yourself C++, and have been doing fine on the activities at the end. I'm having trouble at this one, though. It's the second activity of hour 5, and I'm sure I've made a beginner's error in my programming, but I can't find it. Here's my program:
#include <iostream>

//declare three different average programs
float average(int first, int second);
float average(long first, long second);
float average(float first, float second);

int main()
{
//declare the variables
int first, second;
long firstl, secondl;
float firstf, secondf;
float mean;
//begin user parts
std::cout << "Enter first number average: ";
std::cin >> first or firstl or firstf;
std::cout << "\nEnter second number to average: ";
std::cin >> second or secondf or secondl;
mean = average(first or firstl or firstf, second or secondf or secondl);
std::cout << mean;
return 0;
}

float average(int first, int second)
{
float mean;
mean = (first + second) / 2;
return mean;
}
float average(long first, long second)
{
float mean;
long firstl, secondl;
mean = (firstl + secondl) / 2;
return mean;
}
float average(float first, float second)
{
float mean, firstf, secondf;
mean = (firstf + secondf) / 2;
return mean;
}

What's happening is, when I enter a long integer or a floating-point value, the second point it asks me to enter a value sets automatically to 1, and the program ends without returning even that average. I couldn't find the solutions where it said to go, so what should I do?
Oh, and a sidenote, I haven't gotten to using the "or" command yet, I just threw that in there as a guess.
Last edited on
USE CODE TAGS

Now for the problem ,
`std::cin >> first or firstl or firstf;`
and
` std::cin >> second or secondf or secondl;`
are invalid.
'or' isn't a keyword and you can't do what you wanted in this way.

Your best bet would be to input the number as a string and decide which type to convert it to.
Ah I see, I'm sorry, didn't realize how that was done, now I do.

Ah I thought so. How would I fix that then? It has to be a variable that can accept an integer, long integer, or a floating-point value. That's the point of the program, to use overloaded functions to write a program that will find the average of two integers, long integers, or floating-point values.

EDIT: Here's the program in the right format.
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` ``````#include //declare three different average programs float average(int first, int second); float average(long first, long second); float average(float first, float second); int main() { //declare the variables int first, second; long firstl, secondl; float firstf, secondf; float mean; //begin user parts std::cout << "Enter first number average: "; std::cin >> first or firstl or firstf; std::cout << "\nEnter second number to average: "; std::cin >> second or secondf or secondl; mean = average(first or firstl or firstf, second or secondf or secondl); std::cout << mean; return 0; } float average(int first, int second) { float mean; mean = (first + second) / 2; return mean; } float average(long first, long second) { float mean; long firstl, secondl; mean = (firstl + secondl) / 2; return mean; } float average(float first, float second) { float mean, firstf, secondf; mean = (firstf + secondf) / 2; return mean; }``````
Last edited on
Topic archived. No new replies allowed.