Strange Output

Hey all me again. '

I' setting up a program for three separate functions and I've got them all done until I fix this problem. I'm sure I'll have 10 more errors xD. The weird thing is that the output is supposed to be giving me a conversation from Fahrenheit to Celsius. Instead of giving me the output of a math problem (Fahrenheit - 32 ) / 1.8, it's giving me characters like 0000B71442. I don't have the code at the moment, but wanted to see if anyone's experienced this. No other bugs.
It could be anything, without any code etc.
most likely you printed a pointer instead of a variable, which is more likely in C (printf) than c++, but possible. Or you could have printed something in hex, again easier to mess up in C than in c++.
If you have the output then you clearly DO have the code. So why not show it?
Sorry what I meant was that I am on a separate computer so I don't have the code to copy and paste here at the moment. I'll try and upload before work tomorrow.
it's giving me characters like 0000B71442
You might be printing the address of a variable when you're supposed to print its value instead.

Without the code though, it's impossible to say with any certainty.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// MajorMalFUNCTION.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <cmath>
using namespace std;

void conversionTemp()
{
    float Fahrenheit = 0;
    float conversionTemp = (Fahrenheit - 32) / 1.8;
}

void moleCalculator()
{
    float grams = 0;
    double moleCalculator = grams / 47.86;
}

void eta()
{
    float min, hours = 0;
    float speed = 0;
    float distance = 0;
    double eta = (distance / speed) * 60;
}
int main()
{
    float Fahrenheit = 0;
    cout << "Please enter temperature to be converted: " << endl;
    cin >> Fahrenheit;
    cout << "The temperature is: " << conversionTemp << endl;

    float grams = 0;
    cout << "Please enter grams of Titanium: " << endl;
    cin >> grams;
    cout << "The number of moles is: " << moleCalculator << endl;

    float speed = 0;
    float distance = 0;
    cin >> speed, distance;
    cout << eta << endl;
        
        return 0;

}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file 
Last edited on
cout << "The temperature is: " << conversionTemp << endl;
----------------------------------------^^^^^^^^^^^^^

that is a function, not a value. so yes, it prints the address here.
one problem is that you use the same name for a function and a variable. That isnt illegal, but its hard to follow.

your function does not return a value, either.
why did you mix float and double (unless have a good reason, use double, no float at all).

start using code tags. People won't help if you keep not using them.

1
2
3
4
5
void conversionTemp()  //is type void.  
{
float Fahrenheit = 0;  //what is the point of this?
float conversionTemp = (Fahrenheit - 32) / 1.8;  //same name as function, confusing. 
}


functions can just return a value (you don't need to put it in an intermediate variable just to return it)

putting it together, lets try this one:
1
2
3
4
5
6
7
8
double conversionTemp(double Fahrenheit) //returns a double, and has a parameter to input into it so it is more useful. 
{
return (Fahrenheit - 32) / 1.8;
}
...
cout << conversionTemp(-40); //hehe...  a bit of  a prank
cout << conversionTemp(85); //a better test

Last edited on
right now you have no idea of scope, using parameters or calling functions
start here http://cplusplus.com/doc/tutorial/functions/
look at the examples and compare against your code

> Instead of giving me the output of a math problem (Fahrenheit - 32 ) / 1.8,
> it's giving me characters like 0000B71442.
c++ is not math, code is not equations
the code is read from top to bottom, each line will be executed sequentially

1
2
3
4
5
void conversionTemp()
{
   float Fahrenheit = 0;
   float conversionTemp = (Fahrenheit - 32) / 1.8;
}
`Fahrenheit' has the value 0, so `conversionTemp' will have -17.778
however, the function ends and you do nothing with those variables
your function does nothing

1
2
3
4
5
6
int main()
{
  float Fahrenheit = 0;
  cout << "Please enter temperature to be converted: " << endl;
  cin >> Fahrenheit;
  cout << "The temperature is: " << conversionTemp << endl;
the `Fahrenheit' variable defined in `main()' is a different variable from the `Fahrenheit' defined in `conversionTemp()'
they have no relationship, they just happen to share the same name
¿so how to pass that value to `conversionTemp()'? use parameters
right now your function have no meanings of communication, it's completely useless

what's more, you never call the `conversionTemp()' function, to call a function you need to use parenthesis


these errors and more would have been kindly identified by your compiler if you bother to activate the warnings and actually read them.



> cin >> speed, distance;
¿did you use commas (,) with cout?

also, `eta' is a terrible function name, please, DEA
Thanks for the help so far. I definitely am only three days into functions and it shows. ne555's comments hit me right in the solar plexus lol.
Topic archived. No new replies allowed.