Hi. Im currently doing my assignment, but Im stuck with something. I need to read data from a txt file and store them in two different arrays, which I did. The txt file contains names and numbers. The problem is, I have to make a function that adds the numbers together to find the total, but the numbers aren't the type int, how can I convert them?
My current code:
#include <stdio.h>
#include <stdlib.h>
FILE *f;
int total = 0;
char city[300];
int population[300];
int total_population(int pop[300]){
int i=0;
int count = 0;
//for( i = 0; i < sizeof(pop); i++) {
while(pop[i]!='\0'){
count += pop[i];
i++;
}
printf("%d", count);
}
main(){
f = fopen("stats-population.txt", "r");
while(!feof(f)){
fscanf(f, "%s %s", city, population);
printf("%s %s\n", city, population);
}
total = total_population(population);
exit(0);
}
Question
Tamilboy86
Hi. Im currently doing my assignment, but Im stuck with something. I need to read data from a txt file and store them in two different arrays, which I did. The txt file contains names and numbers. The problem is, I have to make a function that adds the numbers together to find the total, but the numbers aren't the type int, how can I convert them?
My current code:
#include <stdio.h> #include <stdlib.h> FILE *f; int total = 0; char city[300]; int population[300]; int total_population(int pop[300]){ int i=0; int count = 0; //for( i = 0; i < sizeof(pop); i++) { while(pop[i]!='\0'){ count += pop[i]; i++; } printf("%d", count); } main(){ f = fopen("stats-population.txt", "r"); while(!feof(f)){ fscanf(f, "%s %s", city, population); printf("%s %s\n", city, population); } total = total_population(population); exit(0); }Thx
Link to comment
Share on other sites
11 answers to this question
Recommended Posts