function initializing

hello everyone... just trying to figure out this issue. I can't seem to get the functions to call out in my main program. It compiles but the output is blank. Again, first time C++ user and this is an elective so don't crucify me if its completely wrong; ha. thanks in advance.

#include <iostream>
#include <string>
#include <cmath>

using namespace std;


int ReadSize(int s);
int CalcArea(int a);
int Perimeter(int p);
int Estimate(int a, int p);
int s;

int main(int argc, char** argv) {
int ms, ma, mp;

int ReadSide(ms);

int CalcArea(ma);

int Perimeter(mp);

return 0;
}


int ReadSide(int s)
{
cout << "Please enter the length of a side of your square room: ";
cin >> s;
return s;
}

int CalcArea(int a)
{
a=s*s;
return a;
}


int Perimeter(int p)
{
p=s*4;
return p;
}

int Estimate(int a, int p)
{
cout << "You will need " << a << " square feet of carpet.\n";
cout << "You will need " << p << " feet of base board." << endl;

}


Last edited on
int ReadSide(ms);
This is not how to call a function.

1
2
int pointlessValueOfNoUse;
int sideValue = ReadSide(pointlessValueOfNoUse);

This is.


I see that you pass a value in to the function ReadSide that is of no use and shouldn't exist. The function would be better written as:

1
2
3
4
5
6
7
int ReadSide()
{
  int s;
  cout << "Please enter the length of a side of your square room: ";
  cin >> s;
  return s;
}


and then called:
int sideValue = ReadSide();


1
2
3
4
5
int CalcArea(int a)
{
a=s*s;
return a;
}

This is bad. functions know about global variables (these are very bad - do not use them), variables that were created inside the function, and variables that were passed into the function. In this function, s is none of these. This function will not work.
Last edited on
as given, 2 of your function calls are commented out.

Are you not seeing the print statement from read side?
What OS? You may need to flush the output, try adding an endl to it:
cout << "Please enter the length of a side of your square room: " << endl;

also readside is going to bug you up in a bit. it will trip you because of the local s vs the global one. As a rule of thumb, having 2 things with the same name in the same scope is a recipe for human confusion. Don't do that. Also globals are poor, and should be avoided.

Do you know pass by reference yet?

for large code blocks use <> code tags (side of this editor).

Putting all that together, lets give this a try:

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
#include <iostream>
#include <string>
#include <cmath>

using namespace std;


int ReadSize(int s);
int CalcArea(int a);
int Perimeter(int p);
int Estimate(int a, int p);
//s removed here

int main(int argc, char** argv) {
int ms, ma, mp;
int s;  //s added here

s = ReadSide(ms); //s gets the result of the function which you discarded before
cout << "s = " << s << endl;

return 0;
}


int ReadSide(int ls) //this is the local s
{
cout << "Please enter the length of a side of your square room: " << endl;
cin >> ls;
return ls;
}


you can flush without endl, I think its cout.flush() if you want it all on one line.
I don't do a lot of console text programming with I/O so forgive me if that isnt exactly right.
Last edited on
jonnin, windows 10; bloodshed C++.

the console pops up, but is blank.
I just finished while you typed, sorry I was editing my post above. try that part see what it gives and let me know if it helped.

also, even if its blank, even in my version, humor it and type in a value for s, see what happens then. It may be sitting there waiting on input.

Oh, there was a bug. You had "int" in your function calls. that does not belong. Fixing mine for that.

and you had readsiZe vs readsiDe

given these 2 bugs (readside / readsize and the int function() ) I don't think it was acutually compiling. So whatever it was doing when you hit 'run' was maybe running a console/batch file with nothing to do? I don't know bloodshed or how it invokes the exe, but it seems like it did not give you an error message when you tried to run the program that didnt compile.

Fix the compiler errors, and any others in the rest of the code, see what happens.
Last edited on
Topic archived. No new replies allowed.