Need desperate C++ help, FH-OT, you've got 50 minutes!

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
First time i've ever used C++, i'm in a lecture at uni atm. I've got to write a program to 'compute the sum, product and average of 5 numbers read in from the keyboard'

heres what i've got

#include<stdio.h>

void main()
{
float Num1, Num2, Num3, Num4, Num5;
float Sum, Product, Average;

printf("Please type in five numbers: ");
//scanf function allows the user to enter 5 numbers and store them as Num1, Num2 etc
scanf("%f%f%f%f%f", &Num1, &Num2, &Num3, &Num4, &Num5);

Sum = Num1+Num2+Num3+Num4+Num5;
Product = Num1*Num2*Num3*Num4*Num5;
Average = Sum/5;

printf("The results are: ");
printf("Sum=...... %f", Sum);
printf("Product=...... %f", Product);
printf("Average=...... %f", Average);

}

why doesn't it work :|
 

Uara

Part of the furniture
Joined
Jul 24, 2004
Messages
2,254
OMG, my mind asplodes :p sorry Aoami I can't help with this. If it was SQL u might have had a better chance ;)
 

crispy

Can't get enough of FH
Joined
Mar 9, 2004
Messages
2,706
Why it not work? Doesnt compile or what?

Bad style to keep variables with capitalized first letter also!
 

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
I don't get an error, it compiles but closes straight away!

This is why it's shit!

it also displays the answer when i type the numbers in but closes straight after
 

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
It was working after all, i was compiling in the wrong mode
 

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
Now i've got 10 mins to write 'a program that will 3 numbers from the keyboard and print the largest one on the screen using 2 variables.' Can someone do it for me? :D
 

Uara

Part of the furniture
Joined
Jul 24, 2004
Messages
2,254
Aoami, didn't you realise your degree would involve this when you signed up :p
 

old.Tohtori

FH is my second home
Joined
Jan 23, 2004
Messages
45,210
I have a solution, but since you were so rude in title, bossy and didn't even say please, meh :D
 

Aoami

I am a FH squatter
Joined
Dec 22, 2003
Messages
11,223
yeah but like i turned up an hour late for the workshop so i'm well behind
 

Syri

FH is my second home
Joined
Jan 4, 2004
Messages
1,019
if you want to have the result display afterwards, just add something at the end to ask the user to press enter to finish, and then have another prompt for input.
I won't do it for you though, besides, you've got most of what you need in your first post ;)
with a console app, which this is classed as, you need some way of telling windows to keep the console open to display the result, otherwise it just closes it straight away. it's like going to the run command on windows and telling it to run "dir". it'll open a window, show a directory listing, and close it straight away.

oh, and just a small tip, for variable names, i find it handy to name them in a way that they stand out as variables. I usually use names like sText for a string, iNumber for an integer bBool for a boolean and so on, a lower case letter, then caps first letter, makes it obvious it's a variable, and what type of variable it is, and also means you don't accidently use a reserved word
 

Dukat

Resident Freddy
Joined
Jan 10, 2004
Messages
5,396
What Syri said! Sorry Aoami I was at work :(

It works, its just closing as soon as its finished so you dont get a chance to see the output.

You've technicly done enough there as it doesnt actually specify anything about the length of time you have to display the results :) I would try to blag my way out!
 

Sharma

Can't get enough of FH
Joined
Dec 22, 2003
Messages
4,678
camelCase ftw.

Doing a similar thing on my degree except it's bog standard C.

I wanna do C++. :(
 

Laddey

FH is my second home
Joined
May 24, 2005
Messages
7,124
You need to type.

I.fuck.nuns.com


Why arn't you on MSN/Facefuck? I want to speak to you, you little fucker!
 

Lamp

Gold Star Holder!!
Joined
Jan 16, 2005
Messages
23,342
Now i've got 10 mins to write 'a program that will 3 numbers from the keyboard and print the largest one on the screen using 2 variables.' Can someone do it for me? :D

What will you say on the day of your wedding ?

I've got all night to shag my new wife silly before we go on honeymoon, but I don't know how to do it. Will someone shag her for me ?
 

crispy

Can't get enough of FH
Joined
Mar 9, 2004
Messages
2,706
What will you say on the day of your wedding ?

I've got all night to shag my new wife silly before we go on honeymoon, but I don't know how to do it. Will someone shag her for me ?

Aha, so lamp thinks coding C++ is like shagging. Should have known!
 

Syri

FH is my second home
Joined
Jan 4, 2004
Messages
1,019
just to add a bit to this for future reference.
Found a more efficient way to do it when i was fishing around for something completely different. there's another library you can include to use some system commands, one of these being a standard pause for a key. i've got a basic example below that uses it.
Code:
#include <stdio.h>
#include <stdlib.h>


char sName [80];

int main()
{
	printf("what is your name? :>");
	scanf("%s",sName);
	printf("\nHello %s \n\n",&sName);
	system("PAUSE");
	return 1;
}
 

Users who are viewing this thread

Top Bottom