Simple Programming Issue: Halp!

thekeybladewars

CAGiversary!
Feedback
4 (100%)
I have never programmed before in my life, but for some reason I found that it really interests me and I would like to become an expert at it one day.

Anyway, this is my first computer science class and as our homework assignment we have to create a simple C++ program that calculates the volume of a semi sphere, the volume of circular cone and calculates the grand total for both volumes. We also have to create a couple of functions named square () and cube (). So far, I suck at creating functions...

Thanks to Prozac, this is more pleasing to the programming eye:
http://rafb.net/p/WKERMe20.html

________________________________________________________________
#include
#include
#include

using namespace std;

double square (double s); // function prototype
double cube (double c); // function prototype



int main ()
{



double height, radius, semisphere, circularcone, totalvolume;
const double PI= 3.141592;
const double s= radius

ofstream outFile;
ofstream inFile;

inFile.open ("input1.txt"); // Reads the radius and height from a file

inFile
 
Last edited by a moderator:
you want to pass in radius there. or you can do:
const double s = radius;
before you call cube and square. s is not defined in the scope of main.

looks like you're writing c and not c++ so you will have to declare all your variables first.
 
[quote name='jarvis307']you want to pass in radius there. or you can do:
const double s = radius;
before you call cube and square. in the scope of main, s is not defined.

looks like you're writing c and not c++ so you will have to declare all your variables first.[/quote]

I'll try that, thank you for looking. And sorry; I should have specified that it was a C++ program!
 
Yea, just make sure to declare your variables earlier in the function (much like prototyping functions earlier in the program) in order to make sure they exist.
 
also, i'm pretty sure your stream isn't reading radius or height into the respective variables, but you can get to that later after you get your code to compile.
 
Alright, I tried that, and it gives me no warnings and a couple of errors.

When I debug, it tells me "the variable 'radius' is being used without being initialized."
 
[quote name='SneakyPenguin']Yea, just make sure to declare your variables earlier in the function (much like prototyping functions earlier in the program) in order to make sure they exist.[/quote]

for c yes, for c++ no.
 
[quote name='jarvis307']also, i'm pretty sure your stream isn't reading radius or height into the respective variables, but you can get to that later after you get your code to compile.[/quote]

Hmm... that's what happened the last time I declared 's', but in a different way. :whistle2:k

And you're completely right, those two warnings were 'radius' and 'height' being local variables without being initialized.

I don't think I'll get credit for the assignment if I submit it like this. Any other suggestions?
 
[quote name='thekeybladewars']Alright, I tried that, and it gives me no warnings and a couple of errors.

When I debug, it tells me "the variable 'radius' is being used without being initialized."[/quote]

cout is wrong, it should be:

cin >> radius >> etc;

You capture IN to the radius.

Edit: Also, it's a file, and cin/cout is for the console. You should be using:
inFile >> radius >> height;

Edit again: inFile should probably be an ifstream, not an ofstrem
 
[quote name='keithp']I see a bright future as a computer programmer... :roll:[/quote]

What's that supposed to mean? :cry: Cut me some slack, I've never programmed before. The only acceptable answer I've received I had already tried differently and I got the same warnings/errors.
 
[quote name='SneakyPenguin']cout is wrong, it should be:

cin >> radius >> etc;

You capture IN to the radius.

Edit: Also, it's a file, and cin/cout is for the console. You should be using:
inFile >> radius >> height;

Edit again: inFile should probably be an ifstream, not an ofstrem[/quote]

Ah, yes, thanks for reminding me. And the same for outFile with cout too, right?
 
[quote name='JolietJake']I'd help, but you've been mean to me lately.:nottalking:

Actually, i've only ever used VB and C#.[/quote]

That's because you're not a real programmer :booty:
 
First, double radiusSquared= cube (s); Squared = Cube? It won't produce errors, but there's a logical flaw there.

Second, as others have said, nowhere do you declare 's' within main(). It needs to be created first. Then store whatever you want to square/cube in it.

Third, if you're trying to read from a file, you aren't doing it right. I've never actually done C++ before (only C) but I know cout isn't what you want.

Finally, http://rafb.net/paste/ is your friend for code pasting. It's much easier to use than just c/p'ing what you have.


 
[quote name='JolietJake']I'd help, but you've been mean to me lately.:nottalking:

Actually, i've only ever used VB and C#.[/QUOTE]

In the same boat as you. I can't really remember much of them also. Really rusty seeing how I never got a job in programming and have not wrote a line of code in over a year. Anyway thekeybladewars if your having issues now just wait till you get to sql. That for me was always a pain in the ass but I think I always made it harder then what it was. Also I was going to class with about 4 hours of sleep if I was luckey.
 
[quote name='sendme']In the same boat as you. I can't really remember much of them also. Really rusty seeing how I never got a job in programming and have not wrote a line of code in over a year. Anyway thekeybladewars if your having issues now just wait till you get to sql. That for me was always a pain in the ass but I think I always made it harder then what it was. Also I was going to class with about 4 hours of sleep if I was luckey.[/quote]
I'm taking a SQL class right now actually, it's been pretty easy to me. I like how much of it is literal English. Like how the % sign is written as PERCENT.
 
[quote name='I_Am_ProZac']First, double radiusSquared= cube (s); Squared = Cube? It won't produce errors, but there's a logical flaw there.

Second, as others have said, nowhere do you declare 's' within main(). It needs to be created first. Then store whatever you want to square/cube in it.

Third, if you're trying to read from a file, you aren't doing it right. I've never actually done C++ before (only C) but I know cout isn't what you want.

Finally, http://rafb.net/paste/ is your friend for code pasting. It's much easier to use than just c/p'ing what you have.



[/quote]


Alright, I've fixed all of that in the main post, and thank you for that logical error fix. Part of that was me rushing, deleting and trying out other functions but I still get two warnings:

warning C4700: uninitialized local variable 'radius' used
warning C4700: uninitialized local variable 'height' used


... argh.


[quote name='wubb']Do your own homework. :booty:[/quote]


I did most of it! :whistle2:#
 
[quote name='sendme']In the same boat as you. I can't really remember much of them also. Really rusty seeing how I never got a job in programming and have not wrote a line of code in over a year. Anyway thekeybladewars if your having issues now just wait till you get to sql. That for me was always a pain in the ass but I think I always made it harder then what it was. Also I was going to class with about 4 hours of sleep if I was luckey.[/QUOTE]


Right there with you. I have a BS in computer science but can't write a simple counter or loop program in C++ to save my life because its been so long. In fact, I've been out of the loop for so long that it worries me that if I change career fields toward my degree that I won't be able to hang.
 
from this:

http://www.cppreference.com/wiki/io/constructors

looks like you should use an ifstream for input:

ifstream fin( "/tmp/data.txt" );
fin >> radius;
fin >> height;

i can't remember what the delimiter is for ifstreams. i think it may just be white space so just put a space or a newline between the radius and the height in your input1.txt file. i'm a bit out of touch w/ the stl classes but you can hit me up on im if you have more questions.
 
[quote name='jarvis307']from this:

http://www.cppreference.com/wiki/io/constructors

looks like you should use an ifstream for input:

ifstream fin( "/tmp/data.txt" );
fin >> radius;
fin >> height;

i can't remember what the delimiter is for ifstreams. i think it may just be white space so just put a space or a newline between the radius and the height in your input1.txt file. i'm a bit out of touch w/ the stl classes but you can hit me up on im if you have more questions.[/quote]

Well, that gets rid of one of the warnings. Good deal, you've helped me out quite a bit and I appreciate it.
 
Cag does have code tags ya know.
Code:
10 PRINT "Oh boy this is some BASIC stuff!"
20 INPUT "Is this awesome, Y/N?:", A$
30 IF A$ = "N" THEN PRINT "Well nuts to you!"
40 IF A$ = "Y" THEN PRINT "HIGH FIVE!"
50 PRINT "Good bye!"
 
[quote name='AGuth']Right there with you. I have a BS in computer science but can't write a simple counter or loop program in C++ to save my life because its been so long. In fact, I've been out of the loop for so long that it worries me that if I change career fields toward my degree that I won't be able to hang.[/quote]
I remember next to nothing from my C# class, i'ts been about a year and a half since i took it and i haven't used it since. My ASP class used VB, so i remember a little more about it.

VB programmers get no respect, no respect at all.:nottalking:
 
[quote name='thekeybladewars']
warning C4700: uninitialized local variable 'radius' used
warning C4700: uninitialized local variable 'height' used

[/quote]

That means exactly what it says... the variables 'radius' and 'height' are being used without ever assigned a value to them. It's always best to initialize your variables to something to avoid buffer errors.
 
I so wish I could be doing that programming assignment right now. :whistle2:( I so miss using C++ in classes.

So much fun to do on Friday nights oh course. ;) :p If I wasn't heading to bed, I'd try to do the same assignment myself right now.

Although when looking back at a C++ Program I did almost 2 years ago, I declared math.h, not cmath.

EDIT: When I used files, I would do ifstream for the input file (I did ofstream for the output file).

Basically, try:

ifstream inFile; //declare input file
inFile >> radius >> height; //read radius and height into variables.

For the function calls, I don't think you need to say
double radiusSquared= square (s); //function call
double radiusCubed= cube (s); //function call

I believe you just need:
radiusSquared= square (s); //function call
radiusCubed= cube (s); //function call

You already said double in the function prototype and the actual function.
 
Last edited by a moderator:
[quote name='The Mana Knight']I so wish I could be doing that programming assignment right now. :whistle2:( I so miss using C++ in classes.

So much fun to do on Friday nights oh course. ;) :p If I wasn't heading to bed, I'd try to do the same assignment myself right now.

Although when looking back at a C++ Program I did almost 2 years ago, I declared math.h, not cmath.

EDIT: When I used files, I would do ifstream for the input file (I did ofstream for the output file).

Basically, try:

ifstream inFile; //declare input file
inFile >> radius >> height; //read radius and height into variables.

For the function calls, I don't think you need to say
double radiusSquared= square (s); //function call
double radiusCubed= cube (s); //function call

I believe you just need:
radiusSquared= square (s); //function call
radiusCubed= cube (s); //function call

You already said double in the function prototype and the actual function.[/quote]

You're right about the ifstream, but wrong about the other shit, those are right, bar using this random variable s. Should be:

Code:
double radiusSquared = square(radius);
double radiusCubed = cube(radius);

You need to call the function with the correct parameter, and unless you declared radiusSquared and radiusCubed earlier, you're going to need to declare a type.

But yea, definately change inFile to being an ifstream, ofstream is for reading only ('out file stream', as opposed to 'in file stream').
 
[quote name='SneakyPenguin']You're right about the ifstream, but wrong about the other shit, those are right, bar using this random variable s. Should be:

Code:
double radiusSquared = square(radius);
double radiusCubed = cube(radius);

You need to call the function with the correct parameter, and unless you declared radiusSquared and radiusCubed earlier, you're going to need to declare a type.

But yea, definately change inFile to being an ifstream, ofstream is for reading only ('out file stream', as opposed to 'in file stream').[/QUOTE]Like I said, I was pretty tired and wasn't completely thinking (I'm posting on very little sleep here, but can't sleep due to something big tomorrow). ;) That is true about the equation though (I really should have caught that). In ways I'm shocked I missed it because I've helped several online friends with C, C++, VB, MATLAB, etc. work, and tons of people in real life (like this one good looking girl I'd love to date right now) with programming. I use to rarely miss an error, but I guess that means I need to start programming daily again. :p
 
[quote name='The Mana Knight'] I use to rarely miss an error, but I guess that means I need to start programming daily again. :p[/quote]

You have to expect stupid mistakes every now and then. How many hours of my life have I wasted rewriting multiple lines of code, just b/c of a stray semicolon, misspelled identifier, or something equally as small.
 
C++ is a major bitch. I can barely make a simple program, it's hard. I just stuck with Visual Basic as my main programming language. Good luck eh.
 
[quote name='Anbu_Zim']C++ is a major bitch. I can barely make a simple program, it's hard. I just stuck with Visual Basic as my main programming language. Good luck eh.[/quote]

C++ isn't THAT hard, the basics are quite simple. It could be worse, it could be C or Assembly.
 
I got Visual Basic down, it's easy. But C++ is just hard for me, I don't know why. Visual Basic's been said to be harder, right?
 
[quote name='SneakyPenguin']C++ isn't THAT hard, the basics are quite simple. It could be worse, it could be C or Assembly.[/QUOTE]

Or it could be Cobalt. An instructor I had showed me a book on that and it just looked like a bitch to write. However jobs in this area are coming up out the ass for that language.
 
[quote name='Anbu_Zim']C++ is a major bitch. I can barely make a simple program, it's hard. I just stuck with Visual Basic as my main programming language. Good luck eh.[/QUOTE]C++ is extremely easy IMO. The only thing that took me a while to understand in C was the Object Oriented stuff using classes and stuff (because it was a bit different at first), but I still did very well with it.
 
[quote name='SneakyPenguin']C++ isn't THAT hard, the basics are quite simple. It could be worse, it could be C or Assembly.[/QUOTE]

C isn't that much harder than C++. But yeah, Assembly is definitely the worse. Unless you try to program in binary. :drool:

Also, OP, I know this is simple, but did you add the semicolon after this line?

const double s= radius

Just thought I'd point that out. Also, instead of the two functions you made, use use pow(). For instance, 2^3, or two to the third power would be, pow(2,3). Much easier. Just be sure to include math.h.
 
[quote name='Access_Denied']C isn't that much harder than C++. But yeah, Assembly is definitely the worse. Unless you try to program in binary. :drool:

Also, OP, I know this is simple, but did you add the semicolon after this line?

const double s= radius

Just thought I'd point that out. Also, instead of the two functions you made, use use pow(). For instance, 2^3, or two to the third power would be, pow(2,3). Much easier. Just be sure to include math.h.[/quote]

I think the idea was she HAD to write the functions, instead of using pow. Probably a way to test if she could write a function.
 
double height, radius, semisphere, circularcone, totalvolume;
const double PI= 3.141592;
const double s= radius

What are you initializing radius as before assigning the constant variable s its value?

Also, s is a horrible variable name. Spell it out.
 
bread's done
Back
Top