case 3 is the troubled zone, an integer array is working, it's fine, but "first" and "last" string arrays aren't working at all. I don't know how to solve it.
I have to use only stdio.h and stdlib.h libraries."first" and "last" are 2d char pointers, I'd like to 20 elements and maximum 30 characters.Adding ,and editing are fine it's working, but "first[temp]=first[temp+1];" this code cause compiler error when I try "strcpy(first[temp],first[temp+1]);" there is no compiler error.
Eventually using strcpy. when I want to delete the strings remain the same, and the integers take the value of 0
int main(){
printf("Welcome to address book ! \n ");
printf("--------------------------------------------------------\n");
int a=0;
int temp=0;
char* first[20][30];
char* last[20][30];
int n[20];
int g=0;
while(a<=1000){
int b=0;
printf("What do you want? \n");
printf("1-New ID\n2-Edit ID\n3-Delete ID\n4-List All ID's\n5-Close the Program\n");
printf("--------------------------------------------------------\n");
scanf("%d",&b);
switch(b){
case 1:
printf("Enter Fist Name \n");
scanf("%s",&first[g]);
printf("Enter Last Name\n");
scanf("%s",&last[g]);
printf("Enter Student Number\n");
scanf("%d",&n[g]);
g++;
a++;
continue;
case 2:
printf("Which user you want to change ?\n");
printf("user sequence :");
scanf("%d",&temp);
temp=temp-1;
printf("Listed First Name : %s\n",first[temp]);
printf("New First Name : ");
scanf("%s",&first[temp]);
printf("Listed Last Name : %s\n",last[temp]);
printf("New Last Name : ");
scanf("%s",&last[temp]);
printf("Listed Number : %d\n",n[temp]);
printf("New Listed Number : ");
scanf("%d",&n[temp]);
temp=0;
a++;
continue;
case 3:
printf("which user do you want to delete :");
scanf("%d",&temp);
if(temp+1<=g){
// first[temp]=first[temp+1];
// last[temp]=last[temp+1];
n[temp]=n[temp+1];
}
else
printf("unavailable");
a++;
continue;
case 4:
printf("--------------------------------------------------------\n");
int u;
for(u=0;u<g;u++){
printf(">%d.user. ",u+1);
printf("%d\t",n[u]);
printf("%s\t",first[u]);
printf("%s\n",last[u]);
}
printf("\n--------------------------------------------------------\n");
a++;
continue;
case 5:
printf("\nThank you for the use this program !\n ");
temp= 1000-a;
a= a+temp;
a++;
continue;
}
}
return 0;}
Related
My array inside case 4 in looping Switch Menu doesn't print/display the value of the last array when the user input goes beyond array[4].
I tried to take the case 4 out and make it a single program to check if it doesn't really work on its own but it works fine, but when I put it back into the Switch, same issue again. I thought that maybe the initialization part is the problem. Help
`
#include <stdio.h>
int main ()
{
char first[20],last[20];
float math,eng,sci,avg;
int a,b,c,d,diff,array[diff],e,i,input;
do{
printf("\nMAIN MENU\n");
printf("[1] Basic Input Output\n[2] Conditional Statement\n[3] Looping Construct\n[4] Array\n[5] About\n[6] Exit");
printf("\n\nChoose: ");
scanf("%d",&input);
printf("\n");
switch (input)
{
case 1:
printf("\nEnter your given name:");
scanf("%s",first);
printf("Enter your surname:");
scanf("%s",last);
printf("\nHello %s %s!\n",first,last);
break;
case 2:
printf("\nEnter your grade in Math:");
scanf("%f",&math);
printf("\nEnter your grade in Science:");
scanf("%f",&sci);
printf("\nEnter your grade in English:");
scanf("%f",&eng);
avg=(math+eng+sci)/3;
if(math>eng&&sci)
{
printf("\nHighest grade is in: Math");
}
if(eng>math&&sci)
{
printf("\nHighest grade is in: English");
}
if(sci>eng&&math)
{
printf("\nHighest grade is in: Science");
}
if(math==eng)
{
printf("\n--Math and English tied grades--");
}
if(math==sci)
{
printf("\n--Math and Science tied grades--");
}
if(eng==sci)
{
printf("\n--Science and English tied grades--");
}
printf("\nYour average in 3 subjects:%f\n",avg);
break;
case 3:
printf("Enter beginning number: ");
scanf("%d",&b);
printf("Enter ending number: ");
scanf("%d",&c);
printf("\nCounting from %d to %d\n",b,c);
while(b<=c)
{
printf("%d ",b);
b++;
}
printf("\n");
break;
case 4:
printf("Enter Starting Series of Numbers: ");
scanf("%d",&a);
printf("Enter Ending Series of Numbers: ");
scanf("%d",&d);
diff=(d-a);
array[diff]=d;
printf("Select Array Value to Display: 0 to %d: ",diff);
scanf("%d",&e);
for(i=0;i<=diff;i++)
{
array[i]=a;
if(i==e)
{
printf("%d\n",array[i]);
}
a++;
}
break;
case 5:
printf("Abouts\n");
break;
case 6:
printf("Thank you!");
break;
}
}while(input != 6);
return 0;
}
`
This should be a comment, but I donĀ“t have enough reputation yet.
As Gerhardh said, the problem is that you use diff before being initialized (which causes undefined behaviour)
As you said, if you move the initialization of array inside the case 4: after diff being set the program should work as intended. If you want to keep it before the switch, try using int* and malloc/free inside the case 4: (and add #include <stdlib.h>) Anyway, you should check diff as positive value before using it to set the size of array
About optimizing the code, you can skip the line array[diff]=d; because you are setting the same value inside the for loop. And you can remove the line a++; if you changes array[i]=a; -> array[i]=a+i;
This is a part of a program that ask the user to put students data with interactive choices.
int main(){
unsigned int choice = 0;
char new_name[7] = {0};
unsigned int num_students = 0;
Student students_list[3] = {0}; // Student is a struct type
do {
printf("Press 1 to add a student, 2 to edit an existing student, 0 to exit and print the results.\n");
printf("Choose option: ");
scanf("%d", &choice);
switch (choice){
case 1:
printf("Enter the serial number (7 digits): ");
scanf("%s", new_name);
students_list[num_students] = create_student(new_name);
printf("Student added\n");
num_students += 1;
break;
case 2:
printf("student list:\n");
for (size_t i=0; i<num_students; ++i){
print_results(students_list, &num_students);
printf("Choose student id to edit: ");
}
break;
case 0:
printf("I'm printing the results... I'm printing the results...");
print_results(students_list, &num_students);
break;
default:
printf("Error, try again.\n");
break;
}
} while (choice != 0);
}
But there must be something wrong.. It's stuck.
Output:
$ ./"strucfirst"
Press 1 to add a student, 2 to edit an existing student, 0 to exit and print the results.
Choose option: 1
Enter the serial number (7 digits): 1234567
Student added
What am I doing wrong? Is it do_while or switch that makes problems?
Full code (in italian :\ ) if maybe the error could be in other parts of the program: https://hastebin.com/eqayepaquh.cpp
Whenever I run this I got something like ' 196875307' as the total, could
someone tell me whats wrong with it.Here I uploaded the whole code.It says my post is mostly code and to add more details,So forgive me for typing these unnecessory things XD
#include <stdio.h>
int room;
char name[20];
int i;
void main()
{
int answr,fc[6],z=0,tot;
char ans;
char food[8][30]={"Bread","Noodles","Salad","Popcorn","Chocolate ice
cream","Vanilla ice cream","Cold Coffee","Milk Shake"};
int price[8]={180,120,65,55,70,70,110,200};
printf("\n *********");
printf("\n MENU CARD");
printf("\n *********\n\n\n\n");
printf("\n Food Code\t\tprice\t\t Food Name\n");
for(i=0;i<8;i++)
{
printf("\n\t\t%d",i+1);
printf("\t\t%d",price[i]);
printf("\t\t%s",food[i]);
}
printf("\n\n\n\t *PRESS 0 TO GO TO THE MAIN MENU\n\t *PRESS 1 TO ORDER
FOOD");
scanf(" %d",&answr);
switch(answr)
{
case 0:
{
printf("Enter the main menu function here");
break;
}
case 1:do
{
printf("ENTER THE FOOD CODE YOU WANT TO HAVE :: ");
scanf(" %d",&fc[z]);
z++;
tot=tot+fc[z];
printf("total so far is %d",tot);
printf("DO YOU WANT MORE(Y/N) ::");
scanf(" %c",&ans);
}while((ans=='y')||(ans=='Y'));
printf("\nEnter your room number:");
scanf(" %d",&room);
printf("\nEnter your name:");
scanf(" %s",&name);
}
}
The issue lies in your do loop. You need to initialize tot to 0, and use the user-inputted "food code" as an array index into your price array. I don't see any use for the "fc" array you've declared. This code should work for case 1 in your switch statement.
Remember that the main function returns an int in C.
do {
tot = 0;
printf("ENTER THE FOOD CODE YOU WANT TO HAVE :: ");
scanf("%d", &z);
if (z < 1 || z > 8) {
printf("Invalid food code\n");
return -1; // main should return int in a C program
}
tot=tot+price[z-1];
printf("total so far is %d\n",tot);
printf("DO YOU WANT MORE(Y/N) ::");
scanf(" %c",&ans);
} while((ans=='y')||(ans=='Y'));
I think that you should increment your counter z after you do the addition of the total with your freshly added element into the array.
1)Get the value by scanf
2)add it by using vet[z]
3)increment z.
When your code hit the sum it will try to access to a segment of memory that is filled with some other values.
help me. I've been assign to make an application for some test result. but why can't I print out what user have inputted at case 2. I've tried other approach but it's not working. this is the closest I could get there. Any help would be appreciated. super thanks +_+
this is my code :
int main(){
int option;
char namamurid[30][15];
int i=0,j=0;
int listening[15];
int reading[15];
int essay[15];
int score[15];
do{ printf("\"Smart English\" Course Center\n********************************\n");
printf("1.Add new data\n2.View data\n3.View summary\n4.Exit\n\n");
printf("your option[1..4]: ");
scanf("%d",&option);
fflush(stdin);
switch(option){
case 1:
do{
printf("Input student's name[1..25 char]: ");
scanf("%[^\n]s",namamurid[i]);
fflush(stdin);
}while(strlen(namamurid[i])<1 || strlen(namamurid[i])>25);
do{
printf("Correct answer for listening section[0..20]: ");
scanf("%d",&listening[i]);
fflush(stdin);
}while(listening[i]<0 || listening[i]>20);
do{
printf("Correct answer for reading section[0..30]: ");
scanf("%d",&reading[i]);
fflush(stdin);
}while(reading[i]<0 || reading[i]>30);
do{
printf("Correct answer for essay section[0..25]: ");
scanf("%d",&essay[i]);
fflush(stdin);
}while(essay[i]<0 || essay[i]>25);
break;
case 2:
printf("Name\t\tListening\tReading\tEssay\tScore\tGrade\n");
for(j=0;j<i;j++)
{
printf("%-1s\t\t%d\t%d\t%d\t%d\n",namamurid[j],listening[j],reading[j],essay[j],score[j]);
}
break;
}
} while(option<1 || option>4 || option !=4);
getchar();
return 0;
}
There is no change to value of variable i which is set to 0 during initialization.Hence it will never enter the for loop in case 2.
#include<stdio.h>
int main(void)
{
int sum(), sub(), mul(), div();
char ans;
printf("\ta) Add\n");
printf("\tb) Sub\n");
printf("\tc) Multiply\n");
printf("\td) Divition\n");
scanf_s("%c",&ans);
printf("\nYour answer is %c",ans);
if((ans == 'A')||(ans == 'a'))
printf("The sum of Two number is %d\n",sum());
else if((ans == 'B')||(ans == 'b'))
printf("The subraction of Two number is %d\n",sub());
else if((ans == 'C')||(ans == 'c'))
printf("The product of two number is %d\n",mul());
else if((ans == 'D')||(ans == 'd'))
printf("The divition of two number is %d\n",div());
else
printf("Enter the valid option\n");
}
int sum()
{
int x,y;
printf(" Addition\n");
printf("Enter the first number = ");
scanf_s("%d",&x);
printf("Enter the second number = ");
scanf_s("%d",&y);
return x+y;
}
int sub()
{
int x,y;
printf(" Subraction\n");
printf("Enter the first number = ");
scanf_s("%d",&x);
printf("Enter the second number = ");
scanf_s("%d",&y);
return x-y;
}
int mul()
{
int x,y;
printf(" Mltification\n");
printf("Enter the first number = ");
scanf_s("%d",&x);
printf("Enter the second number = ");
scanf_s("%d",&y);
return x*y;
}
int div()
{
int x,y;
printf(" Divition\n");
printf("Enter the first number = ");
scanf_s("%d",&x);
printf("ENter the second number = ");
scanf_s("%d",&y);
return x/y;
}
scanf_s("%c",&ans);
the function scanf does not recognize my input
whats wrong in the code
when i use
ans=getchar();
is working perfect but.. the scanf does not recognize what i input
can any one explain the problem in the code iam using visual studio 2010
To scan a single char you have to put a blank space before %c:
scanf(" %c", &char);
The blank space is needed to consume any \n char in the input buffer. You must put the blank space just for char type.