4 Replies - 249 Views - Last Post: 10 December 2012 - 01:33 PM Rate Topic: -----

#1 Livingjunk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-November 12

Error in my read data

Posted 10 December 2012 - 11:15 AM

char temp[20];
sscanf(buffer, "%[^\n]", temp, buffer);
		if(isdigit(temp[0]))// check if what read in is a digit
		{
			while(r < 8)
			{
				sscanf(temp, "%*d%d", olympians[i].base_values[r]);

				for(int c = 0; c < 12; c++){
					sscanf(temp, "%d", olympians[i].scores[r][c]);
					tempScore[r] += olympians[i].scores[r][c];
				}
				tempScore[r] /= 12;
				if((olympians[i].base_values[r]) <= 3.1){
					tempScore[r] /= 2;
					tempScore[r] += (olympians[i].base_values[r]);
				}
				else
					tempScore[r] += (olympians[i].base_values[r]);

				olympians[i].total_scores[i] += tempScore[r]; 
			r++;
			}
		}


txt line im trying to read in:
1 7.5 -3 -3 -3 -3 -3 -3 -3 -3 -3 -3 -3 -3

is something wrong with
sscanf(temp, "%*d%d", olympians[i].base_values[r]);


because the compiler crash whenever i debugged this part

Is This A Good Question/Topic? 0
  • +

Replies To: Error in my read data

#2 Livingjunk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-November 12

Re: Error in my read data

Posted 10 December 2012 - 11:25 AM

View PostLivingjunk, on 10 December 2012 - 11:15 AM, said:

char temp[20];
sscanf(buffer, "%[^\n]", temp, buffer);
		if(isdigit(temp[0]))// check if what read in is a digit
		{
			while(r < 8)
			{
				sscanf(temp, "%*d%d", olympians[i].base_values[r]);

				for(int c = 0; c < 12; c++){
					sscanf(temp, "%d", olympians[i].scores[r][c]);
					tempScore[r] += olympians[i].scores[r][c];
				}
				tempScore[r] /= 12;
				if((olympians[i].base_values[r]) <= 3.1){
					tempScore[r] /= 2;
					tempScore[r] += (olympians[i].base_values[r]);
				}
				else
					tempScore[r] += (olympians[i].base_values[r]);

				olympians[i].total_scores[i] += tempScore[r]; 
			r++;
			}
		}


txt line im trying to read in:
1 7.5 -3 -3 -3 -3 -3 -3 -3 -3 -3 -3 -3 -3

is something wrong with
sscanf(temp, "%*d%d", olympians[i].base_values[r]);


because the compiler crash whenever i debugged this part

nvm i see my error, why would olympians[i].name) work when reading string but why would i need &olympians[i].base_values[r]) for base value?
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,423
  • Joined: 23-August 08

Re: Error in my read data

Posted 10 December 2012 - 12:55 PM

Without seeing your struct definition, I will assume that the name member is a C-string (an array of characters). The name of an array is a pointer to the first element of the array, so the & is not necessary. Other non-pointer variables require the & to give scanf the address of the variable to be written to.

EDIT: You said name was a string.
Was This Post Helpful? 0
  • +
  • -

#4 Livingjunk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-November 12

Re: Error in my read data

Posted 10 December 2012 - 01:11 PM

View PostJackOfAllTrades, on 10 December 2012 - 12:55 PM, said:

Without seeing your struct definition, I will assume that the name member is a C-string (an array of characters). The name of an array is a pointer to the first element of the array, so the & is not necessary. Other non-pointer variables require the & to give scanf the address of the variable to be written to.

EDIT: You said name was a string.

typedef struct skating
{
	char name[MAX];
	float base_values[MAX];
	int scores[8][MAX];
	float total_scores[MAX];
	GOE execution;
}SKATING;


Here is my structure
so what you are saying is that SKATING olympians[MAX] point to name[0] so i dont need and & there but i do when i want to read in base_values and etc...?
Was This Post Helpful? 0
  • +
  • -

#5 Livingjunk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-November 12

Re: Error in my read data

Posted 10 December 2012 - 01:33 PM

for(int c = 0; c < 12; c++)
{
        sscanf(buffer, "%d%[^\n]", &olympians[i].scores[r][c]);
	olympians[i].execution.grade[r][1]+= olympians[i].scores[r][c]
}



What is wrong with this code? i did the same thing for base_value and it work but scores doesn't read in -3 like i want it to.

This post has been edited by JackOfAllTrades: 10 December 2012 - 04:10 PM
Reason for edit:: Fixed code tags

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1