d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Why Does My Program Only Read In Once?
Add Reply New Topic New Poll
Member
Posts: 3,706
Joined: Mar 8 2009
Gold: 17,175.10
Sep 17 2012 09:04am
The goal of this assignment is to ask the user how many states they want to compare, collect the data on state name and electoral votes in that state
compare it to the highest state electoral votes, (or if it is the first state, make it the highest) and print out the state with the highest electoral votes, the amount of votes,
and how much it exceeds the average of the other states electoral votes. Here is a sample run:
How many states are you entering?
4
Enter the state name and electoral votes for state 1:
Michigan 11
Enter the state name and electoral votes for state 2:
Florida 28
Enter the state name and electoral votes for state 3:
California 55
Enter the state name and electoral votes for state 4:
Northcarolina 15

Response:
California with 55 is the largest. It exceeds the others’
average by 37.


Currently when I enter "Michigan 11" it prints :
Enter the state name and electoral votes for state 2
Enter the state name and electoral votes for state 3
Enter the state name and electoral votes for state 4

without letting me read in the values. I looked over it for an hour or so and checked out a few of my professor's example programs but I cannot find the solution to this error.
:wallbash:
:wallbash:
:wallbash:
Any assistance would be great
:)

Code
#include <stdio.h>

int main() {
   int i, state,electoral_votes=3,num_states;
   int greatest_state, greatest_electoral=2, losertally=0, loseravg, avg_diff;
   
   printf("how many states are you entering?\n");
   scanf("%d", &num_states);
   
   for (i=1;i<=num_states;i++)    {
      printf("Enter the state name and electoral votes for state %d\n",i);
      scanf("%d",&state);
      scanf("%d",&electoral_votes);
             
      if (electoral_votes>greatest_electoral){
         losertally+=greatest_electoral;
         greatest_electoral=electoral_votes;
         greatest_state=state;}
         
      else
          losertally+=electoral_votes;
          }
      loseravg=losertally/num_states;
      avg_diff=greatest_electoral-loseravg;
     
      printf("%d with %d is the largest. It exceeds the other's average by %d.\n",greatest_state,greatest_electoral,avg_diff);
      system("pause");
      return 0;
}
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 17 2012 09:42am
Edit nvm not sure

I personally would have made the scan part (for loop) into a function and have main call to it with a loop then add the if statements and prints after (also in main)

This post was edited by Eep on Sep 17 2012 09:47am
Member
Posts: 3,706
Joined: Mar 8 2009
Gold: 17,175.10
Sep 17 2012 09:45am
Quote (Eep @ Sep 17 2012 07:42am)
You are missing a close bracket some where afaik



Ive moved close brackets around many times and I cannot seem to get it to work. Leaving things open, closing them, and different combinations.

My loop is going through 4 times like I ask, it's just the second third and fourth times around it doesn't take the user's input.. I'm baffled.

Maybe I need to use getchar instead of scanf?

This post was edited by TheDiscoveryChannel on Sep 17 2012 09:47am
Member
Posts: 3,706
Joined: Mar 8 2009
Gold: 17,175.10
Sep 17 2012 09:52am
Quote (Eep @ Sep 17 2012 07:42am)
Edit nvm not sure

I personally would have made the scan part (for loop) into a function and have main call to it with a loop then add the if statements and prints after (also in main)



So create an array first by doing scanf then calling/comparing them after? Not completely familiar with the lingo, but I think I understand what you're saying. I'll try to write it that way.
Member
Posts: 3,706
Joined: Mar 8 2009
Gold: 17,175.10
Sep 17 2012 10:07am
I established arrays for the both state and the electoral_votes variables. It still only reads in one time. ALSO!! Even though I am only entering michigan if all my calculations were correct I should get
Michigan with 11 is the largest. It exceeds the others’
average by 0.
Instead I get incredible values instead of michgan and 0.

Code
#include <stdio.h>

int main() {
   int i,electoral_votes[55],num_states;
   
   printf("how many states are you entering?\n");
   scanf("%d", &num_states);
   
   int greatest_electoral, runningtotal=0,loser_total, loseravg, avg_diff;
   int greatest_state, state[num_states];
   
   for (i=1;i<=num_states;i++)    {
      printf("Enter the state name and electoral votes for state %d\n",i);
      scanf("%d",&state[i]);
      scanf("%d",&electoral_votes[i]);}
     
   for (i=1;i<=num_states;i++)     {    
       if(electoral_votes[i]>greatest_electoral) {
           greatest_electoral=electoral_votes[i];
           greatest_state=state[i];
         }
      runningtotal+=electoral_votes[i];        
      }
         
      loser_total=runningtotal-greatest_electoral;  
      loseravg=loser_total/num_states-1;
      avg_diff=greatest_electoral-loseravg;
     
      printf("%d with %d is the largest. It exceeds the other's average by %d.\n",greatest_state,greatest_electoral,avg_diff);
      system("pause");
      return 0;
}



/e this wont work though because im getting a number value when I call the array, not the state.

This post was edited by TheDiscoveryChannel on Sep 17 2012 10:12am
Member
Posts: 3,706
Joined: Mar 8 2009
Gold: 17,175.10
Sep 17 2012 11:30am
Found my issues.
I changed to
Code
char greatest_state[20], state[20];

because it was not outputting my state name

I changed
Code
scanf("%s %d",state,&electoral_votes);

because i had the %d instead of %s when using scanf.

I was also trying to set
Code
greatest_state=state;

which was clearly not working...
So I changed to
Code
strcpy(greatest_state,state);


This post was edited by TheDiscoveryChannel on Sep 17 2012 11:30am
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 17 2012 02:16pm
Good stuff dude. I am less familiar with c than c++. Didn't notice the error because of that
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll