2 Replies - 1620 Views - Last Post: 16 February 2011 - 07:16 PM Rate Topic: -----

#1 jasminetea  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-February 11

"pointer from integer without a cast" & percentage problem

Posted 16 February 2011 - 05:51 PM

Instructions:


In the file quizin1.dat (which you should cp to your directory), each
line represents a voting card of a registered voter. The character '1'
in the i-th indicates that the vote was cast for the i-th candidate. The
first 10 columns were for ten candidates for the Governor of California.
The names of these candidates are in the input file quizin2.dat (cp it to
your directory).

Open the files quizin1.dat and quizin2.dat by using the file
pointers *finput1 and *finput2. Use the file pointer *foutput1 for the
output file quizout1.dat. Read the file quizin1.dat by using fgets().
Count the total number of voting cards, and the number of votes
cast for each of ten candidates. Use a character array: votes[80]
for votes, and an integer array: cand[10] for the candidates.
Print these results on the screen, and the numerical results in the output
file quizout1.dat. The printout on the screen should be as shown below,
while the file quizout1.dat should contain integers cand[0], cand[1],
..., cand[9] (either in one line or in separate lines).

Close the input file quizin1.dat and the output file quizout1.dat.

Then, open the created output file quizout1.dat for reading by
using the file pointer *finput3. Read the numerical data from this
file by using while(fscanf(finput3,...), and calculate the percentage
of votes that each of the ten candidates received.
Within the while(fscanf(finput3),...) loop,
use fgets(cand_name,40,finput2) in conjunction with
sscanf(cand_name, "%s %s", name, surname) to scan the surname of the
candidate and make the printout on the screen and in the output file
quizout2.dat, poited to by the pointer *foutput2, as shown below.
Use the format %-16s when printing the string surname.

Close the files quizout1.dat and quizin2.dat.

...........................................................................

Your output should look like:

Total number of voting cards: 60

Votes for candidate 1: 2
Votes for candidate 2: 7

...........................
...........................
...........................

Votes for candidate 9: 8
Votes for candidate 10: 5

Percent votes for candidate Adams is 3.33 %
Percent votes for candidate Camacho is 11.67 %

.........................................................
.........................................................
.........................................................

Percent votes for candidate Tatro is 13.33 %
Percent votes for candidate Jordan is 8.33 %

Total percent votes for all candidates is 100.00 %
*/







My code so far:


#include<stdio.h>
int main(void)
{
FILE *finput1, *finput2, *finput3, *foutput1, *foutput2;
char votes[80], name[41], surname[41];
int k, total_votes, cand[10], cand_name=0;
double percent, total_percent;

finput1 = fopen("quizin1.dat", "r");
finput2 = fopen("quizin2.dat", "r");
finput3 = fopen("quizout1.dat", "r");
foutput1 = fopen("quizout1.dat", "w");
foutput2 = fopen("quizout2.dat", "w");
printf("\n");

while(fgets(votes,80,finput1)!=NULL)
{
fscanf(finput1, "%lf", votes);
for(k=0, total_votes=0; k<81; k++)
total_votes+= votes[k];

printf("\nTotal number of voting cards: %d\n\n",total_votes);
fprintf(foutput1, " \nTotal number of voting cards: %d\n\n",total_votes);


printf("Votes for candidate %d: %lf\n", k, votes);
fprintf(foutput1, "Votes for candidate %d: %lf\n", k, votes);
}
fclose(finput1);
fclose(finput2);
fclose(foutput1);


while(fscanf(finput3, "%-16s %lf", &surname, &percent)!=EOF)
{
sscanf(cand_name, "%-16s", surname);
cand_name++;

printf("Percent votes for candidate %-16s is %lf\n", surname, percent);
fprintf(foutput2, "Percent votes for candidate %-16s is %lf\n", surname,
percent);

total_percent+= percent;

printf("\nTotal percent votes for all candidates is %.2lf\n\n",
total_percent);
fprintf(foutput2, "\nTotal percent for all candidates is %.2lf\n\n",
total_percent
);

}
fclose(finput3);
fclose(foutput2);
}









What happens when I try to run it:

warning: passing argument 1 of 'sscanf' makes pointer from integer without
a cast


referring to the line: sscanf(cand_name, "%-16s", surname);






What I still need to figure out how to do:
1) How to get the % of votes for each candidate
2) How to have #s (1,2,3,4, etc.) instead of surnames for "Votes for
candidate 1: .. etc"
3) How to total up the number of percents for all candidates (unless what
I did was right?)
4) Does my program even make sense? :(

Is This A Good Question/Topic? 0
  • +

Replies To: "pointer from integer without a cast" & percentage problem

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 3848
  • View blog
  • Posts: 11,775
  • Joined: 25-December 09

Re: "pointer from integer without a cast" & percentage problem

Posted 16 February 2011 - 06:51 PM

First how about a little formatting? See this link: Formatting.

Also in future please post the complete error/warning message. These messages have information embedded in them to aid trouble shooting, like line number where the error is located.

For your error you might want to look at this link for scanf. Your first parameter (cand_name) is an int, the first parameter should be a const char *.


Jim
Was This Post Helpful? 0
  • +
  • -

#3 jasminetea  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-February 11

Re: "pointer from integer without a cast" & percentage problem

Posted 16 February 2011 - 07:16 PM

View Postjimblumberg, on 16 February 2011 - 06:51 PM, said:

First how about a little formatting? See this link: Formatting.

Also in future please post the complete error/warning message. These messages have information embedded in them to aid trouble shooting, like line number where the error is located.

For your error you might want to look at this link for scanf. Your first parameter (cand_name) is an int, the first parameter should be a const char *.


Jim




I used that link & fixed the scanf part of my program & finally got my program to run. However, the output is:


Total number of voting cards: 1297

Votes for candidate 81: -22448876808220807962529583088995439096615792407600282129336949436234159000948934326926501448318246540952131304630302316066923535973716158964542838155275809961324380500196459441187641975705960841205313618462799439028880765622855144215231180136884428458456403899813948804833479402158873762063607757532280389632.000000

Total number of voting cards: 1643

Votes for candidate 81: -22448876808220807962529583088995439096615792407600282129336949436234159000948934326926501448318246540952131304630302316066923535973716158964542838155275809961324380500196459441187641975705960841205313618462799439028880765622855144215231180136884428458456403899813948804833479402158873762063607757532280389632.000000

Total number of voting cards: 1757

Votes for candidate 81: -22448876808220807962529583088995439096615792407600282129336949436234159000948934326926501448318246540952131304630302316066923535973716158964542838155275809961324380500196459441187641975705960841205313618462799439028880765622855144215231180136884428458456403899813948804833479402158873762063607757532280389632.000000


and it keeps repeating with different numbers for a long while. At the end, it says:


Total number of voting cards: 1675

Votes for candidate 81: -22448876808220807962529583088995439096615792407600282129336949436234159000948934326926501448318246540952131304630302316066923535973716158964542838155275809961324380500196459441187641975705960841205313618462799439028880765622855144215231180136884428458456403899813948804833479402158873762063607757532280389632.000000
Segmentation Fault (core dumped)


How do I get it to print the proper number of voting cards? I think I'm supposed to print that after the forloop so it doesn't repeat so many times but my result is completely wrong...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1