Having a small a problem with an array value here is the code:i want the array(totalwin) to store the value of the calulation each time it repeats say 10 times.I tried using the for loop but the values are not stored in the array what am i doing wrong.

// Simple program that uses calulates addition and subtraction using uses//
//user imput //
//The user imput is stored in an array//

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

int main( void) 
{
    int num[5]; // the array user imput will be stored in//
    int sum,minus,cnt,totalsum[5];
    int choice,count=0; // count used to check how many times the program runs//
    char repeat;



    //user asked to choose add or subtract//
    printf("Press [1] to add or [2] to subtract ");
    scanf("%d" ,&choice);

    //Error message if the user does not choose 1 or 2//
    while (choice != 1 && choice != 2)
     {
         printf("Enter correct choice ");
         scanf("%d",&choice);
     }  

    do {

        //For loop used to store user imput//
        for (cnt = 0 ; cnt < 2 ; cnt ++) 
        {
            printf("\nEnter your value ");
            scanf("%d",&num[cnt]);
        }


        //Imput goes in case based on the user choice//
        switch(choice)
        { // 
           case 1: 
               sum = num[0]+ num[1];
               printf("\nAddition amounts to %d",sum);
               for (cnt =0 ; cnt < 3 ;cnt ++)
               totalsum[cnt]=sum ;
               break;             
           case 2:
               minus = num[0] - num[1] ;
               printf("\nSubtraction amounts to %d",minus);
               break;
        }

        printf("\nDo you want to restart ? yes[y] or no[n] ");
        scanf("%s",&repeat);
        count = count + 1;
    }while (repeat == 'y' && count < 3 );

    printf("\nValue is %d\n",totalsum[cnt]);
    getch();
}

Edited 4 Years Ago by deceptikon: Changed to discussion thread and fixed indentation

i want the array(totalwin) t

You mean totalsum, there is no variable named totalwin

line 46 is saving the same value in totalsum three times. If you want each element of that array to contain the value of sum each time the loop repeats then you need a loop counter that only changes once each time the loop repeats. For example:

int main()
{
   int index = 0;
   ...
   ...
   totalsum[index] = num[0] + num[1];
   printf("Addition amounts to %d\n", totalsum[index]);
   index++;
   ...
   ...
}

Edited 4 Years Ago by Ancient Dragon

yes the variable is totalsum thanks for the input gonna try and hash it out

does it matter if i use dev c ++ portable or borland c to compile ? because i made a simple program that repeats and displays how much time the prgram in run. Dev c portable says 0 and borland calculates the counter pefectly.

Edited 4 Years Ago by Stpdoug

here it is

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

int main (void)

{ // Begin //

 // Variables//
 int pbet;
 int cnt = 0;
 char c;


  do {

    //  If the user choses to restart the program,it will begin from here//
   printf( "\nEnter the amount you want to bet:  ");
   scanf ("%d",&pbet);

    cnt ++;

    printf("\nRepeat ? [y] or [n]");
    scanf("%s",&c);

  } while (c == 'y' && cnt< 5 );// bonus player can only play 5 times//
 // Ending of restart loop//



printf(" Program ran %d",cnt);
printf(" times ");

 getch();
}

ok but when using "%c" line 23 is being by passed and variable c and num1 are reading line 19

Edited 4 Years Ago by Stpdoug

thanks can you tell me why adding that spaces made a difference ?

%c only takes a single character so it takes the enter key pressed from the previous input from "scanf ("%d",&pbet);" , we put a space there to solve that

thanks for all the help that program was just a simplification of a larger program the values are being stored in the arrays correctly i only need to determine the largest and samllest among them then display

i only need to determine the largest and samllest among them then display

you could do it by using a loop where you compare a current number from the next then replace it if the numbered compared is higher or lower until the last element to find highest or lowest number
or use a sorting algorithm (like a simple bubble sort should be fine for this) then you can print the beginning and end of the array where the highest and lowest number can be found

Edited 4 Years Ago by zeroliken

This article has been dead for over six months. Start a new discussion instead.