Can someone help me with my program. I have the code but there are some logical errors. The program should contain 100 numbers that have a menu, first the user should input a number then the menu will show: 1. it adds again a number, 2. it displays all the inputted numbers, 3. it will display the sum of all the numbers, 4. the program will exit. This is the code i had created i don't know I'm correct with the code in the case 2,3,4.

#include<stdio.h>
void main(void)
{
int sub,total=0,choice;
int numbers[100];
add:
do{
clrscr();
printf("Key-in numbers:\n\n");
for(sub=0;sub<100;sub++)
{
scanf("%d",&numbers[sub]);
printf("MENU");
printf("[1] Add Number.");
printf("[2] Display all numbers.");
printf("[3] Display Total.");
printf("[4] Exit.");
printf("Select your choice: ");
scanf("%d",&choice);
switch(choice){
case 1:
goto add;
break;
case 2:
printf("Number [%d]: %d",sub,numbers[sub]);
break;
case 3:
total=total+numbers[sub];
printf("\nTotal: %d.",total);
break;
case 4:
return 0:
default:
printf("Invalid Choice!");
getch();
goto add;
}
}
}while(choice!=3);
getch();
return 0;
}

First, format your code so we can follow it. That is necessary if you need others to read your code.

If you aren't sure if you are correct for 2, 3, & 4, try running the program. That will tell you whether you are correct or not.

check this ,u need to use int main when u are using return 0; so try to go through gud tutorials ,rest google for the syntax or online comolplier..

#include<stdio.h>
int main()
{
int sub,total=0,choice;
int numbers[100];
add:
do{
printf("Key-in numbers:\n\n");
for(sub=0;sub<100;sub++)
{
scanf("%d",&numbers[sub]);
printf("MENU");
printf("[1] Add Number.");
printf("[2] Display all numbers.");
printf("[3] Display Total.");
printf("[4] Exit.");
printf("Select your choice: ");
scanf("%d",&choice);
switch(choice){
case 1:
goto add;
break;
case 2:
printf("Number [%d]: %d",sub,numbers[sub]);
break;
case 3:
total=total+numbers[sub];
printf("\nTotal: %d.",total);
break;
case 4:
default:
printf("Invalid Choice!");
return 0;
getch();
goto add;
}
}
}while(choice!=3);
getch();
return 0;
}

sorry im just a newbie here. when i select 2, which is to display all the numbers that were inputted, it only shows the last number that was inputted. When i select 3, which is to show the sum of all the inputted numbers, it doesn't add all the numbers that had been inputted. My code runs, but it has logical errors. Can someone help me?

Yes. Start by rereading my previous post -- first line..

#include<stdio.h>
void main(void)
{
int sub,total=0,choice;
int numbers[100];
add:
do{
clrscr();
printf("Key-in numbers:\n\n");
for(sub=0;sub<100;sub++)
{
scanf("%d",&numbers[sub]);
printf("MENU");
printf("[1] Add Number.");
printf("[2] Display all numbers.");
printf("[3] Display Total.");
printf("[4] Exit.");
printf("Select your choice: ");
scanf("%d",&choice);
switch(choice){
case 1:
goto add;
break;
case 2:
printf("Number [%d]: %d",sub,numbers[sub]);
break;
case 3:
total=total+numbers[sub];
printf("\nTotal: %d.",total);
break;
case 4:
return 0:
default:
printf("Invalid Choice!");
getch();
goto add;
}
}
}while(choice!=3);
getch();
return 0;
}
void main(void)

'main' should always return an 'int'(see hereor hereor here,...the list will go "beyond infinity" ).
It's better not to use labels and goto statements as they make your program's control flow hard to follow

goto add;

(see here).Here in your case you can use 'continue' statement, it will do the same thing as you're doing with those 'goto' statements.

clrscr();

and

getch();

These are non-standard functions, so your code is better off without them.As you haven't included "conio.h" header, they will not work anyway.

Take help from online compiler i didn't understand your code.

thanks for all the responses. it'll be a big help. :)

@saravinuya: well, did you really understand what WaltP tried to say about FORMATTING ?? Did you read the gidnetwork link she provided? ( click here, if you haven't)
So next time you post try to keep that in mind...because its very difficult for us to read un-formatted code. Hopefully, you'll understand.

I'm not sure why you have a menu option to input data, yet at the same time, you're prompting for input with each iteration of the for loop. It seems like you'd need just the one loop.

int idx = 0, numbers[100] = { 0 };

do {
   // show menu
   // get choice
   switch(choice) {
      case 1 : // prompt user for input
               // store input (if valid) in numbers array and increment idx
               break;
      case 2 : // idx would contain all significant data entered thus far
               for(i=0; i < idx; ++i) { 
                  printf("%d ", numbers[i]);
               }
               break;
      case 3 : // basically the same as 2, loop through the valid data
               // sum the numbers array and output the result
               break;
      case 4 : // display exit message or such
               break;
      default : // ...
   }
} while(idx < 100 && choice != 4); // short circuit loop on idx being valid

I'd be leery of using scanf; especially with numbers. Maybe something like that.
HTH

uhmm, it's ok i understand. sorry for my faults. :)

#include<stdio.h>
void main(void)
{
   int sub,total=0,choice;
   int numbers[100];
   add:
   do{
      clrscr();
      printf("Key-in numbers:\n\n");

      for(sub = 0 ; sub < 100 ; sub++)
     {
         scanf("%d",&numbers[sub]);
         printf("MENU");
         printf("[1] Add Number.");
         printf("[2] Display all numbers.");
         printf("[3] Display Total.");
         printf("[4] Exit.");
         printf("Select your choice: ");
         scanf("%d",&choice);

         switch(choice){
            case 1:
              goto add;
         break;
            case 2:
              printf("Number [%d]: %d",sub,numbers[sub]);
         break;
            case 3:
              total = total + numbers[sub];
              printf("\nTotal: %d.",total);
         break;
            case 4:
              return 0:
         default:
              printf("Invalid Choice!");
              getch();
              goto add;
         }
     }
}while(choice!=3);
getch();
return 0;
}
commented: Never be sorry for your faults. They make you you. And you can learn to fix them to make you better ;o) +17
case 1 : 
      goto add;
   break;

this bit will basically reset everything but total. each time your user chooses "add a number," he or she will be prompted to enter a number starting at numbers[0] and prompted for a choice; by the looks of things, the same thing will occur for the default case.

case 2 :
      printf("Number: [%d] %d\n", sub, numbers[sub]);
   break;

this one will display whatever value happens to be stored in numbers[sub]; you won't get all of the values currently stored in your array. you'll need a loop inside of the case starting at 0 and going through sub - 1.

case 3 : 
      total = total + numbers[sub];
      printf("\nTotal: %d.", total);
   break;

this one may or may not have an accurate total. depending on what menu option the user chooses, total may be some arbitrary value of re-initialized array entires and not the direct sum of numbers[0-99].

I haven't tried your code, but I suspect the only way to get a meaningful total would be to choose menu option 2 each iteration of sub.

HTH

seprate a for loop first.
1) which takes only numbers.

for(int i=0;i<100;i++)
{

}

total will not include all numbers because for 1 loop of sub you are calculating and displaying only 1 number.

so separate your loops.

1)
for(sub=0;sub<100;sub++)
{
scanf("%d",&number[sub]);
}

and then within case '3':

for(sub=0;sub<count;sub++) //count is the total number you have entered.
{
total=total+number[sub];
}
//and display total over here.
//count is the total number you have entered.I hope u know how get the value for "count"...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.