'Printing the data received from the user to the screen with c
I'm trying to write a code that prints the contact information (name, surname, identity number, occupation) taken from the user one by one using a switch case, then collectively prints it to the screen in the form of a list. Could you help?
#include<stdio.h>
int main(){
int secim,loop=0;
long long int tc[3][2];
int i,j,n=0;
char ad[3][2];
char soyad[3][2];
char meslek[3][2];
while(loop==0){
printf("\nSeciminizi Yapiniz:\n[1]BILGI GIRISI\n[2]LISTELEME\n[3]ARAMA\n[4]BILGI SILME\n[5]SONLANDIR\n"); scanf("%d",&secim);
switch(secim){
if(secim==1){
n++;
}
case(1):// bilgi girisi
for(j=0;j<1;j++){
for(i=0;i<1;i++){
printf("Ad Giriniz: "); scanf("%s",&ad[i][j]);
printf("Soyad Giriniz: "); scanf("%s",&soyad[i][j]);
printf("Meslek giriniz "); scanf("%s",&meslek[i][j]);
printf("TC giriniz "); scanf("%lld",&tc[i][j]);
}
}
break;
case(2)://listeleme
printf("-------------\n");
for(j=0;j<n;j++){
for(i=0;i<1;i++){
printf("Adi: %s\n",ad[i][j]);
printf("Soyadi: %s\n",soyad[i][j]);
printf("Meslegi: %s\n",meslek[i][j]);
printf("Tc Numarasi: %lld\n",tc[i][j]);
printf("-------------\n");
}
}
break;
case(3):
printf("3.secim\n");
break;
case(4):
printf("4.secim\n");
break;
case(5):
loop=1;
break;
break;
}
}
return 0;
}
I think I'm making a mistake with arrays or for loop AND MY OTHER CODE
#include<stdio.h>
#include<string.h>
int main(){
int secim,loop=0;
char pool[]="Kullanici bilgileri \n", users[100];
int i;
while(loop==0){
printf("\nSeciminizi Yapiniz:\n[1]BILGI GIRISI\n[2]LISTELEME\n[3]ARAMA\n[4]BILGI SILME\n[5]SONLANDIR\n"); scanf("%d",&secim);
switch(secim){
case(1):// bilgi girisi
printf("\n Ad: "); scanf("%s", &users); strcat(pool,users);
printf("\n Soyad: "); scanf("%s", &users); strcat(pool,users);
printf("\n Meslek: "); scanf("%s", &users); strcat(pool,users);
printf("\n Kimlik: "); scanf("%s", &users); strcat(pool,users);
break;
case(2)://listeleme
printf("%s",pool);
break;
}
return 0;
}
}
Solution 1:[1]
In the first code section you compiler should warn you that the if will never execute. The if should be outside the switch.
switch(secim){
if(secim==1){
Code like strcat(pool,users); wil overflow the end of pool. Ditto for char ad[3][2]; and similar. There should be enough space to copy the strings is you add space for them in the declaration:
char pool[1024]="Kullanici bilgileri \n",
The compiler should warn you about weirdness at the location of these problems in the first code segment. Consider warnings as errors.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
