scanf with floats giving problems, alternatives?

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
58
59
60
61
62
63
64
65
66
#include "stdafx.h"

float get_temp() {

	float temp_grid[6][8] = {
		{ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
		{ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
		{ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
		{ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
		{ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
		{ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 } 
	};

	float top = 0.0; float bot = 0.0; float left = 0.0; float right = 0.0;
	int rows = 0;
	int cols = 0;

	printf("Enter a float temperature (example 5.36) for the top side of the metal plate:  \n");
	scanf("%.2f", &top);

	for (cols = 0; cols < 8; cols++) {
		temp_grid[0][cols] = top;
	}

	printf("Enter another float temperature for the bottom side of the metal plate:  \n");
	scanf("%.2f", &bot);

	for (cols = 0; cols < 8; cols++) {
		temp_grid[5][cols] = bot;
	}

	printf("Enter another float temperature for the left side of the metal plate:  \n");
	scanf("%.2f", &left);

	for (rows = 1; rows < 5; rows++) {
		temp_grid[rows][0] = left;
	}

	printf("Enter another float temperature for the right side of the metal plate:  \n");
	scanf("%.2f", &right);

	for (rows = 1; rows < 5; rows++) {
		temp_grid[rows][7] = right;
	}

	
	for (rows = 1; rows < 5; rows++) {
		for (cols = 1; cols < 7; cols++) {
			temp_grid[rows][cols] = (temp_grid[rows][cols - 1] + temp_grid[rows - 1][cols] + temp_grid[rows][cols + 1] + temp_grid[rows + 1][cols]) / 4;	/*Left + right + top + bot / 4*/
			printf("%.2f ", temp_grid[rows][cols]);
		}

		return 0.0;

	}


}

int main()
{
	
	get_temp();

    return 0;
}


Hey all, I'm having trouble with getting user input for this program..
The goal is to prompt the user for 4 input temperatures for the sides of a grid, and then set the grid side values to those temperatures.

When I run the program, as soon as I enter the first temperature the program prints the other 3 prompts and then ends... not sure why.

I know 'scanf' is generally frowned upon, but with my limited knowledge I can't think of an alternative to fix this. Any help is much appreciated, thanks!
Last edited on
There is nothing wrong with scanf. You just don't use it properly.
scanf("%.2f", &top); should be scanf("%f", &top);
.2 works only for printf.
wow thanks, that's embarrassing lol.

For some reason the grid im trying to print out now will only print around 6 values and then stop though..
For some reason the grid im trying to print out now will only print around 6 values and then stop though..

You return within the rows loop. Swap lines 53 and 55.

You presumably need a line feed after finishing a row.
Last edited on
As I seem to being saying a lot these days: always check the value scanf returns, just to see if it worked:

1
2
3
4
5
6
7
int ScanfArgs = 0;

ScanfArgs = scanf("%f", &top);

if (ScanfArgs != 1) {
  printf("Scanf arguments not read in correctly");
}


Ideally this should be put in a loop and in a function.

If you are scanning multiple values, a switch is handy.

I know 'scanf' is generally frowned upon, but with my limited knowledge I can't think of an alternative to fix this.


Do you want to write C code, or C++ ?

C++ has std::cout and std::cin

Prefer double rather than float. the latter only has 6 or 7 significant figures and the precision is easily exceeded. double has 15 or 16 sf.
Last edited on
Topic archived. No new replies allowed.