Totally screwed on comp science course!

Pages: 123
Also, for another program can someone please tell me why the last line of code shows the winning percentage as a 0 instead of a decimal?

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
#include <stdio.h>
#include <stdlib.h>
#include "genlib.h"
#include "simpio.h"
#include <iostream>
#include <cmath>

int main()
{
	int l1, l2, l3, l4, gp, gw, p, gp2, gp3, gp4, gw2 ,gw3, gw4, p2, p3, p4;
	printf("Enter first name: League1 \n");
	printf("Enter games played for League1: ");
	gp=GetInteger();
	printf("Enter games won for League1: ");
	gw=GetInteger();
	printf("\n");

	printf("Enter second name:League2 \n");
	printf("Enter games played for League2: ");
	gp2=GetInteger();
	printf("Enter games won for League2: ");
	gw2=GetInteger();
	printf("\n");

	printf("Enter third name: League3: \n");
	printf("Enter games played for League3: ");
	gp3=GetInteger();
	printf("Enter games won for League3: ");
	gw3=GetInteger();
	printf("\n");

	printf("Enter fourth name: League 4 \n");
	printf("Enter games played for League4: ");
	gp4=GetInteger();
	printf("Enter games won for League4: ");
	gw4=GetInteger();
	printf("\n\n");

	p=gw/gp;


printf("Name		Games Played	Games Won	Winning Percentage\n");
printf("----		------------    ---------	------------------\n");
printf("League1		%d		%d		%d\n", gp, gw, p);
	
	
Last edited on
I'm guessing, just guessing that you are printing a decimal value instead of a float or double, maybe try %f instead of %d?

oh and at the very least make p a float

That's just from first glance.
Last edited on
how would i assign p to be a float?

float p; ?

When I do that and run it, it says:error C2371: 'p' : redefinition; different basic types
change this:
int l1, l2, l3, l4, gp, gw, p, gp2, gp3, gp4, gw2 ,gw3, gw4, p2, p3, p4;

to this

1
2
int l1, l2, l3, l4, gp, gw, gp2, gp3, gp4, gw2 ,gw3, gw4, p2, p3, p4;
float p;


-------------

and this:
printf("League1 %d %d %d\n", gp, gw, p);

to this

printf("League1 %d %d %f\n", gp, gw, p);

and it ought to work
Now, it gets the decimal point and stuff to show, but it only displays 0.00000, instead of the real numerical decimal.

In this case, i used 35 and 23, respectively, for the variables gp gw.
another program i have to do has to get the volume of either a box, cube, or cylinder. The user is prompted to enter one of the 3 strings: box, cube, cylinder. Then the program is asks for the values (Ex: side length). Then the program displays the shape's volume.

Here is my faulty code:


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
#include <stdio.h>
#include "simpio.h"
#include "strlib.h"
#include "random.h"

std::string shape;

int main()
{
	int i, cube, box, b1, pi, cylinder, stringshape, s, s2, height;
	printf ("Please enter the shape:");
	stringshape=GetLine();
	s=GetInteger();
	pi=3.1415167;
	if (shape=="cube")
	{
	b1=s*s*s;
	printf("The volume of a cube with side=%d, is %d, s, b1.");
	}
	else if (shape=="cylinder")
	{
		height=GetInteger();
		b1= pi*height*(s*s);
		printf("The volume of a cylinder with side=%d, is %d, s, b1.");
	}
	else if (shape=="box")
	{
		height=GetInteger();
		s2= GetInteger();
		b1= s*s2*height;
		printf ("The volume of a box with sides %d and %d is %d, s, s2, b1.");
	}
	return 0;
}

Last edited on
Shouldn't you say shape = GetLine();?
for that program it is giving me:error C2039: 'string' : is not a member of 'std'

even when i switched to shape = GetLine();
Don't forget to #include <string>
Do you have #include<string> in your headers? If you don't then that will probably be the problem.
Just out of curiosity, why do you use GetLine() and GetInteger()?

Edit: Too slow.
Last edited on
even when i put the <string> in the headers, std::shape; says that error: namespace std has no member "shape"

@browni3141 I use GetLine to enter the strings: box, cylinder, cube and the GetInteger to get variable numbers
Last edited on
No member "space"? The compiler's right, there is no member called space, at least as far as I know. There also doesn't appear to be any "space" in the code you gave us, which makes me wonder if it's in any of the other three headers.

-Albatross
What should I do then?
I'd suggest that you search though your code and your headers for the piece of text "space". Maybe it's just a typo somewhere that can be fixed.

EDIT: Without the quotes.

-Albatross
Last edited on
sorry i was thinking wrong. it is shape, not space. my bad.

shape is located at shape=GetLine
Maybe you should show your code again and paste the errors as it stands so that it's easier to see the problem.
Last edited on
Okay... then I'm not sure what to do from here.

Could you please show the updated code? It might help me bit a wittle bit less confused. :P

EDIT: Late.

-Albatross
Last edited on
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
#include <stdio.h>
#include "simpio.h"
#include "strlib.h"
#include "random.h"
#include <string>

std::shape;   [b]error: namespace std has no member "shape"[/b]

int main()
{
	int i, cube, box, b1, pi, cylinder, shape, s, s2, height;
	printf ("Please enter the shape:");
	shape=GetLine();
	s=GetInteger(); [b]error: a value of type "String" cannot be assigned to an entity of type "int"[/b]
	pi=3.1415167;
	if (shape=="cube")[b] Error: opperand types are uncompatable ("int" and "const char*")
[/b]	{
	b1=s*s*s;
	printf("The volume of a cube with side=%d, is %d, s, b1.");
	}
	else if (shape=="cylinder")  [b] Error: opperand types are uncompatable ("int" and "const char*")
[/b]b]
	{
		height=GetInteger();
		b1= pi*height*(s*s);
		printf("The volume of a cylinder with side=%d, is %d, s, b1.");
	}
	else if (shape=="box") [b] Error: opperand types are uncompatable ("int" and "const char*")
[/b]
	{
		height=GetInteger();
		s2= GetInteger();
		b1= s*s2*height;
		printf ("The volume of a box with sides %d and %d is %d, s, s2, b1.");
	}
	return 0;
I kinda thought so, but I wanted to be sure. :)

Line 7: Change it back to what you had before.
Line 11: Delete "space" from your lineup of integers.

-Albatross
Last edited on
Great! It seems to be working! Thank you very much!

Now, if you could be so kind, could you take a look at the posts above, and explain to me why the decimal for the program shows as 0.00? :)
Last edited on
Pages: 123