struct{
char nome[30],rua [50],bairro [20],cidade [30],se [3] ;
int na, exc, numero;
long int CEP;
}typedef endereco;
main(){
endereco agenda[20];
int i, j, opc, opc1=0, opc2, eopc ;
char pnome[30], pbairro[20], pcidade[30], enome[30];
while (opc!=4){
system("cls");
printf("Agenda facs, digite a opção: \n|\t1-incluir\t|\n|\t2-Consultar\t|\n|\t3-excluir\t|\n|\t4-sair\t |\nDigite a opção ");
scanf("%d",&opc);
fflush(stdin);
switch (opc){
case 1:
system("cls");
printf("Digite os Dados a seguir para incluir no # %d :\n",opc1+1);
agenda[opc1].na=opc1+1;
agenda[opc1].exc=0;
printf("Digite o nome: ");
gets(agenda[opc1].nome);
printf("Digite a Rua: ");
gets(agenda[opc1].rua);
printf("Digite o Bairro: ");
gets(agenda[opc1].bairro);
printf("Digite o numero: ");
fflush(stdin);
scanf("%d",&agenda[opc1].numero);
fflush(stdin);
printf("Digite a Cidade: ");
gets(agenda[opc1].cidade);
printf("Digite a Sigla do Estado: ");
gets(agenda[opc1].se);
printf("Digite o CEP: ");
fflush(stdin);
scanf("%d",&agenda[opc1].CEP);
fflush(stdin);
opc1++;
break;
case 2:
system("cls");
printf("\n|\t1-Nome\t|\n|\t2-Bairro\t|\n|\t3-Cidade\t|\n|\tDigite a opção ");
scanf("%d",&opc2);
printf("\t|\n");
fflush(stdin);
switch (opc2){
case 1:
system("cls");
printf("Qual o nome a procurar? ");
gets(pnome);
for(i=0;i<20;i++){
if(strcmp(pnome,agenda[i].nome)==0){
printf("\n\tNumero Agenda: ");
puts(agenda[i].na); printf("\n\tNome: ");
printf("\n\tNome: ");
puts(agenda[i].nome);
printf("\n\tRua: ");
puts(agenda[i].rua);
printf("\n\tBairro: ");
puts(agenda[i].bairro);
printf("\n\tNumero: ");
puts(agenda[i].numero);
printf("\n\tCidade: ");
puts(agenda[i].cidade);
printf("\n\tEstado: ");
puts(agenda[i].se);
printf("\n\tCEP: ");
puts(agenda[i].CEP);
printf("\n\t______________");}}
break;
case 2:
system("cls");
printf("Qual o Bairro a procurar? ");
gets(pbairro);
for(i=0;i<20;i++){
if(strcmp(pbairro,agenda[i].bairro)==0){
printf("\n\tNumero Agenda: ");
puts(agenda[i].na); printf("\n\tNome: ");
printf("\n\tNome: ");
puts(agenda[i].nome);
printf("\n\tRua: ");
puts(agenda[i].rua);
printf("\n\tBairro: ");
puts(agenda[i].bairro);
printf("\n\tNumero: ");
puts(agenda[i].numero);
printf("\n\tCidade: ");
puts(agenda[i].cidade);
printf("\n\tEstado: ");
puts(agenda[i].se);
printf("\n\tCEP: ");
puts(agenda[i].CEP);
printf("\n\t______________");}}
break;
case 3:
system("cls");
printf("Qual a Cidade a procurar? ");
gets(pcidade);
for(i=0;i<20;i++){
if(strcmp(pbairro,agenda[i].cidade)==0){
printf("\n\tNumero Agenda: ");
puts(agenda[i].na);
printf("\n\tNome: ");
puts(agenda[i].nome);
printf("\n\tRua: ");
puts(agenda[i].rua);
printf("\n\tBairro: ");
puts(agenda[i].bairro);
printf("\n\tNumero: ");
puts(agenda[i].numero);
printf("\n\tCidade: ");
puts(agenda[i].cidade);
printf("\n\tEstado: ");
puts(agenda[i].se);
printf("\n\tCEP: ");
puts(agenda[i].CEP);
printf("\n\t______________");}}
break;}
case 3:
system("cls");
printf("Digite o Numero da Agenda que deseja excluir");
gets(enome);
for(i=0;i<20;i++){
if(strcmp(enome,agenda[i].nome)==0){
printf("\n\tNumero Agenda: ");
puts(agenda[i].na);
printf("\n\tNome: ");
puts(agenda[i].nome);
printf("\n\tRua: ");
puts(agenda[i].rua);
printf("\n\tBairro: ");
puts(agenda[i].bairro);
printf("\n\tNumero: ");
puts(agenda[i].numero);
printf("\n\tCidade: ");
puts(agenda[i].cidade);
printf("\n\tEstado: ");
puts(agenda[i].se);
printf("\n\tCEP: ");
puts(agenda[i].CEP);
printf("\n\t______________");}}
printf("\n\tDigite o Numero Agenda do Nome na qual voce quer excluir: ");
fflush(stdin);
scanf("%d",&i);
fflush(stdin);
if(agenda[i].exc==0){
printf("\n\t%d . %s \n\tDeseja excluir esse contato?\n\t1-Sim\n\t2-Nao\n\tOpcao: ",agenda[i].na,agenda[i].nome);
fflush(stdin);
scanf("%d",&eopc);
fflush(stdin);
switch (eopc){
case 1:
agenda[i].exc=1;
printf("\t\nNumero excluido !");
break;
case 2:
printf("\t\nNumero nao excluido !");
break;}}}}
system("PAUSE");
return 0;
}
Hello :D
This program was build, or trying to, be a notebook that you can save contacts.
But i'm getting this warning, and when the 'puts' come in the program, it crashes.
Someone can help ?
[Warning] passing arg 1 of `puts' makes pointer from integer without a
cast
Thanks.
puts(agenda[i].na);
In your code agenda[i].na is an int. You can't print it with puts, try printf:
printf("%d\n", agenda[i].na);
and when the 'puts' come in the program, it crashes.
You are tricking puts into using a random integer as a pointer.
Related
This program writes, edits and deletes tasks from a txt file. I'm having problems with my add task function (case (1)) in this situation. Here's a snippet of the code:
switch(choice)
{
case '1':
system("cls");
fseek(fp,0,SEEK_END);
another = 'y';
while(another == 'y')
{
printf("\nEnter Task_Name: ");
scanf("%s", &t.Task_Name);
printf("\nEnter Leader: ");
scanf("%s", &t.Leader);
printf("\nEnter L_Email: ");
scanf("%s", &t.L_Email);
printf("\nEnter Member: ");
scanf("%s", &t.Member);
printf("\nEnter Mem_Email: ");
scanf("%s", &t.Mem_Email);
printf("\nEnter Begin_date(dd/mm/yyyy): ");
scanf("%d/%d/%d", &dd,&mm,&yyyy);
printf("\nEnter End_date(dd/mm/yyyy): ");
scanf("%d/%d/%d", &dd,&mm,&yyyy);
fwrite(&t,recsize,1,fp);
printf("\nAdd another task(y/n) ");
fflush(stdin);
another = getche();
}
break;
However, when I attempt to add a date with the above code I get gibberish in return. For example, when I type 11/11/1111 as the start date and 11/11/1111 as the end date, I get 6421994 and 6422005 in return. The same numbers shows up when I input any other date too.
To keep things reproducible I will have to include the entire code;
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include<dos.h>
int main()
{
FILE * fp, * ft;
char another, choice;
struct task
{
char Task_Name[50], Leader[50], L_Email[50], Member[50], Mem_Email[50], Begin_date[11], End_date[11];
};
struct task t;
int mm, dd, yyyy;
char TaskName[40];
long int recsize;
fp = fopen("task.txt","rb+");
if(fp == NULL)
{
fp = fopen("task.txt","wb+");
if(fp == NULL)
{
printf("Cannot open file");
exit(1);
}
}
recsize = sizeof(t);
while(1)
{
system("cls");
printf("\n\t **** Welcome to Personal System Management ****");
printf("\n\n\n\t\t\tMAIN MENU\n\t\t=====================\n\t\t[1] Add a new Task\n\t\t[2] View all Task\n\t\t[3] Update Task\n\t\t[4] Delete Task\n\t\t[5] Exit Program\n\t\t=================\n\t\t");
printf("Enter choice: ");
choice = getche();
switch(choice)
{
case '1':
system("cls");
fseek(fp,0,SEEK_END);
another = 'y';
while(another == 'y')
{
printf("\nEnter Task_Name: ");
scanf("%s", &t.Task_Name);
printf("\nEnter Leader: ");
scanf("%s", &t.Leader);
printf("\nEnter L_Email: ");
scanf("%s", &t.L_Email);
printf("\nEnter Member: ");
scanf("%s", &t.Member);
printf("\nEnter Mem_Email: ");
scanf("%s", &t.Mem_Email);
printf("\nEnter Begin_date(dd/mm/yyyy): ");
scanf("%d/%d/%d", &dd,&mm,&yyyy);
printf("\nEnter End_date(dd/mm/yyyy): ");
scanf("%d/%d/%d", &dd,&mm,&yyyy);
fwrite(&t,recsize,1,fp);
printf("\nAdd another task(y/n) ");
fflush(stdin);
another = getche();
}
break;
case '2':
system("cls");
rewind(fp);
printf("Task Name|Leader|Leader Email|Member|Member Email|Begin Date|End Date");
while(fread(&t,recsize,1,fp)==1)
{
printf("\n%s %s %s %s %s %d %d",t.Task_Name,t.Leader,t.L_Email,t.Member,t.Mem_Email,t.Begin_date,t.End_date);
}
getch();
break;
case '3':
system("cls");
another = 'y';
while(another == 'y')
{
printf("Enter the task name you want to update: ");
scanf("%s",TaskName);
rewind(fp);
while(fread(&t,recsize,1,fp)==1)
{
if(strcmp(t.Task_Name,TaskName) == 0)
{
printf("Enter new Member name: ");
scanf("%s",&t.Member);
printf("Enter new Member Email: ");
scanf("%s",&t.Mem_Email);
printf("Enter new End Date(dd/mm/yyyy): ");
scanf("%d/%d/%d",&dd,&mm,&yyyy);
fseek(fp,-recsize,SEEK_CUR);
fwrite(&t,recsize,1,fp);
break;
}
}
printf("\nUpdate another task(y/n)");
another = getche();
}
break;
case '4':
system("cls");
another = 'y';
while(another == 'y')
{
printf("Enter the task name you want to delete: ");
scanf("%s",TaskName);
ft = fopen("Temp.dat","wb");
rewind(fp);
while(fread(&t,recsize,1,fp) == 1)
{
if(strcmp(t.Task_Name,TaskName) != 0)
{
fwrite(&t,recsize,1,ft);
}
}
fclose(fp);
fclose(ft);
remove("task.txt");
rename("Temp.dat","task.txt");
fp = fopen("taxt.txt", "rb+");
printf("Delete another task(y/n)");
fflush(stdin);
another = getche();
}
break;
case '5':
fclose(fp);
exit(0);
}
}
return 0;
}
You're never copying the dates into the structure.
printf("\nEnter Begin_date(dd/mm/yyyy): ");
scanf("%d/%d/%d", &dd,&mm,&yyyy);
sprintf(t.Begin_date, "%02d/%02d/%04d", dd, mm, yyyy);
printf("\nEnter End_date(dd/mm/yyyy): ");
scanf("%d/%d/%d", &dd,&mm,&yyyy);
sprintf(t.End_date, "%02d/%02d/%04d", dd, mm, yyyy);
And since these are strings, you need to use %s when printing them, not %d.
printf("\n%s %s %s %s %s %s %s",t.Task_Name,t.Leader,t.L_Email,t.Member,t.Mem_Email,t.Begin_date,t.End_date);
void addadmin() {
FILE *f;
int menu;
int total;
int hargaTriple = 3000000;
int hargaDoubleBed = 2000000;
int hargaTwin = 1500000;
int hargaSingleBed = 1000000;
int a=0;
int i=0;
time_t t;
time(&t);
char test;
f=fopen("add.txt","a+");
if(f==0) {
f=fopen("add.txt","w+");
system("cls");
printf("Please hold on while we set our database in your computer!!");
printf("\n Process completed press any key to continue!!! ");
getch();
}
while(1) {
system("cls");
for(i=0;i<100;i++)
printf("-");
printf("\n Current date and time : %s",ctime(&t));
for(i=0;i<100;i++)
printf("-");
printf("\n");
for(i=0;i<100;i++)
printf("=");
printf("\n");
printf("\t\t\t\t\t|ROOM RESERVATION|\t\t\t\t\t\n");
for(i=0;i<100;i++)
printf("=");
printf("\n");
printf("\n NAME : ");
fflush(stdin);
scanf("%[^\n]%*c", s.name,20);
printf("\n--------------------------------------------------");
printf("\n GENDER [M/F] : ");
scanf_s("%s", s.gender, 2);
printf("\n--------------------------------------------------");
printf("\n AGE : ");
scanf_s("%d", &s.age);
printf("\n--------------------------------------------------");
printf("\n ADDRESS : ");
fflush(stdin);
scanf("%[^\n]%*c", s.address);
printf("\n--------------------------------------------------");
printf("\n PHONE NUMBER : ");
scanf("%s",s.phonenumber);
printf("\n--------------------------------------------------");
printf("\n NATIONALITY : ");
scanf("%s",s.nationality);
printf("\n--------------------------------------------------");
printf("\n E-MAIL ADDRESS : ");
scanf(" %s",s.email);
printf("\n--------------------------------------------------");
printf("\n ROOM TYPE:");
printf("\n------------------------------");
printf("\n Enter 1 -> EXECUTIVE TRIPLE");
printf("\n------------------------------");
printf("\n Enter 2 -> DOUBLE ROOM");
printf("\n------------------------------");
printf("\n Enter 3 -> TWIN ROOM");
printf("\n------------------------------");
printf("\n Enter 4 -> SINGLE ROOM");
printf("\n------------------------------");
printf("\n Enter your choice: ");
scanf_s("%s", s.typeroom, 10);
printf("\n--------------------------------------------------");
printf("\n NUMBER OF ROOM : ");
scanf_s("%d", &s.numberofroom);
printf("\n--------------------------------------------------");
for(a=0; a<s.numberofroom; a++)
printf("\n ROOM NUMBER : %s",s.roomnumber[a]);
fflush(stdin);
printf("\n--------------------------------------------------");
printf("\n PERIOD [DAYS] : ");
scanf_s("%d", &s.period);
printf("\n--------------------------------------------------");
printf("\n ARRIVAL [DD/MM/YYYY] : ");
scanf("%s",&s.arrivaldate);
for(i=0;i<100;i++)
printf("=");
printf("\n");
printf("\n\n ROOM BOOKING DATA:");
printf("\n--------------------------------------------------");
printf("\n NAME : %s", s.name);
printf("\n--------------------------------------------------");
printf("\n GENDER : %s", s.gender);
printf("\n--------------------------------------------------");
printf("\n AGE : %d", s.age);
printf("\n--------------------------------------------------");
printf("\n ADDRESS : %s",s.address);
printf("\n--------------------------------------------------");
printf("\n PHONE NUMBER : %s",s.phonenumber);
printf("\n--------------------------------------------------");
printf("\n NATIONALITY : %s",s.nationality);
printf("\n--------------------------------------------------");
printf("\n E-MAIL ADDRESS : %s",s.email);
printf("\n--------------------------------------------------");
for(a=0; a<s.numberofroom; a++)
printf("\n ROOM NUMBER : %s",s.roomnumber[a]);
printf("\n--------------------------------------------------");
printf("\n ROOM TYPE : %s", s.typeroom);
printf("\n--------------------------------------------------");
printf("\n NUMBER OF ROOM : %d", s.numberofroom);
printf("\n--------------------------------------------------");
printf("\n PERIOD : %d", s.period);
printf("\n--------------------------------------------------");
printf("\n ARRIVAL : %s",&s.arrivaldate);
printf("\n--------------------------------------------------");
I've tried to change roomnumber to numberofrooms but the output is just repetitive room number. The roomnumber was supposed to contain the room numbers but from what I understand, the roomnumber array is just a blank/empty array. So, how to change the output from null to number of rooms that i wanted?
Im doing a struct to register students´ info, previously I asked the user how many he wanted to register and proceeded to do it with a for loop:
case 1:
printf("How many students you want to register?:(no more than 10) \n");
scanf("%d", &num);
fflush(stdin);
printf("\n");
for(int i=0;i<num;i++){
alumno[i]=llenarInfo(alumno[i]);
}
printf("---------------------\n");
but how can I do it 1 at a time? I tried increasing i after the loop for but if I register 2 or more and want to print them all it only prints the last one and when asking for the name of the student (atm of filling the info) it automatically jumps to the next member of the struct:
struct Usuario llenarInfo(struct Usuario alumno){
printf("Dame el nombre: ");
gets(alumno.nombre);
fflush(stdin);
printf("");
jumps this^^
printf("Dame el codigo de alumno: ");
scanf("%d", alumno.codigo);
fflush(stdin);
printf("");
printf("Dame el email: ");
scanf("%s", alumno.email);
fflush(stdin);
printf("");
printf("Que carrera estudia?: ");
scanf("%s", alumno.carrera);
fflush(stdin);
printf("");
printf("Cual es el sexo? H Hombre- M Mujer: ");
scanf("%s", alumno.sexo);
fflush(stdin);
printf("");
printf("Cual es la edad?: ");
scanf("%s", alumno.edad);
fflush(stdin);
printf("\n");
return alumno;
but how can i do it 1 at a time?
from your question I think the fflush is not working
you can define a simple flush function to use it instead
void _flush() {
int c;
while((c = fgetc(stdin)) != EOF) {
if(c == '\n') break;
}
}
besides you should use fgets instead of gets, because gets doesn't check for overflows and that makes it dangerous, and it's no more in C standard!
I'm trying to put a loop on my code(a simple calculator), where it restarts if you try to divide some number per 0. I'm here for hours but can't manage to see the problem. I'm a beginner by the way.
Tried using do while, using while and using if, every time the program simple ignores my condition. Now it is looping every time even without the conditions that I established being fulfilled.
How do I establish "N2=0 and operation being '/'" as a condition?
#include <stdio.h>
#include <stdlib.h>
int main() {
float N1, N2, resultado;
char operacao;
printf("Ola usuario! \n");
printf("Para comecar, por favor digite o primeiro numero: ");
scanf_s("%f", &N1);
printf("Digite o segundo numero: ");
scanf_s("%f", &N2);
printf("\n Qual operacao aritmetica deseja realizar hoje? \n");
printf("Escolha um dos 4 simbolos'+,-,* ou /': ");
scanf_s(" %c", &operacao);
printf("\n");
while ((N2 = 0) || (operacao == '/')); {
printf("Operacao nao suportada! \n");
printf("Digite novamente o segundo numero: \n");
scanf_s("%f", &N2);
printf("Escolha um dos 4 simbolos'+,-,* ou /': ");
scanf_s(" %c ", &operacao);
}
switch (operacao)
{
case '+':
resultado = N1 + N2;
break;
case '-':
resultado = N1 - N2;
break;
case '*':
resultado = N1 * N2;
break;
case '/':
resultado = N1 / N2;
break;
}
printf("\n O resultado da operacao escolhida e: %.1f", resultado);
printf("\n\n");
system("pause");
return 0;
}
Change the while loop as follows
while (N2 == 0 && operacao == '/') {
printf("Operacao nao suportada! \n");
printf("Digite novamente o segundo numero: \n");
scanf("%f", &N2);
printf("Escolha um dos 4 simbolos'+,-,* ou /': ");
scanf("%c", &operacao);
}
Removed the stray ; at the end of
while (N2 == 0 && operacao == '/')
That caused an infinite loop preventing it to proceed to the actual body we wanted to execute.
Changed the while loop condition as we want it to execute only if N2 is zero 'AND' operacao is '/' not in any other case.
First, the program wants you to enter the student name. When I want to show the student's name in the bottom with %s, xcode always tells me to replace with %c. Can you give me the solutions how to show the student name as it was entered, not using %c? Thank you.
#include <stdio.h>
void showthelastvalue () {
char name1, name2, name3, name4;
int value1a, value1b, value1c, value1d;
int value2a, value2b, value2c, value2d;
int value3a, value3b, value3c, value3d;
int value4a, value4b, value4c, value4d;
printf("\nenter student name-1 : ");
scanf(" %s", name1);
printf("enter student name-2 : ");
scanf(" %s", name2);
printf("enter student name-3 : ");
scanf(" %s", name3);
printf("enter student name-4 : ");
scanf(" %s", name4);
printf("\nEnter student grade-1 %s\n", name1);
printf("grade ke 1 : ");
scanf("%d", &grade1a);
printf("grade ke 2 : ");
scanf("%d", &grade1b);
printf("grade ke 3 : ");
scanf("%d", &grade1c);
printf("grade ke 4 : ");
scanf("%d", &grade1d);
printf("\nEnter student grade- 2 %s\n", name2);
printf("grade ke 1 : ");
scanf("%d", &grade2a);
printf("grade ke 2 : ");
scanf("%d", &grade2b);
printf("grade ke 3 : ");
scanf("%d", &grade2c);
printf("grade ke 4 : ");
scanf("%d", &grade2d);
printf("\nEnter student grade- 3 %s\n", name3);
printf("grade ke 1 : ");
scanf("%d", &grade3a);
printf("grade ke 2 : ");
scanf("%d", &grade3b);
printf("grade ke 3 : ");
scanf("%d", &grade3c);
printf("grade ke 4 : ");
scanf("%d", &grade3d);
printf("\nEnter student grade- 4 %s\n", name4);
printf("grade ke 1 : ");
scanf("%d", &grade4a);
printf("grade ke 2 : ");
scanf("%d", &grade4b);
printf("grade ke 3 : ");
scanf("%d", &grade4c);
printf("grade ke 4 : ");
scanf("%d", &grade4d);
printf("\nThe grade of 4 students: \n");
printf(" %s %d %d %d %d\n", name1, grade1a, grade1b, grade1c, grade1d);
printf(" %s %d %d %d %d\n", name2, grade1a, grade1b, grade1c, grade1d);
printf(" %s %d %d %d %d\n", name3, grade1a, grade1b, grade1c, grade1d);
printf(" %s %d %d %d %d\n", name4, grade1a, grade1b, grade1c, grade1d);
averange1 = (grade1a + grade1b + grade1c + grade1d) / 4;
averange2 = (grade2a + grade2b + grade2c + grade2d) / 4;
averange3 = (grade3a + grade3b + grade3c + grade3d) / 4;
averange4 = (grade4a + grade4b + grade4c + grade4d) / 4;
printf("Last grade of 4 studentsgra :\n");
printf("Last grade from student1 %s = %d\n", name1, averange1);
printf("Last grade from student2 %s = %d\n", name2, averange2);
printf("Last grade from student3 %s = %d\n", name3, averange3);
printf("Last grade from student4 %s = %d\n", name4, averange4);
}
int main() {
int choose;
printf("Welcome!\n");
do {
printf("Choose anda :\n");
printf("1. Show the last grade\n");
printf("2. Show the grade\n");
printf("3. Show the Table\n");
printf("4. Exit\n");
printf("\nWhat will you choose ? ");
scanf("%d", &choose);
switch (choose) {
case 1:
showthelastgrade ();
break;
case 4:
printf("Thank you /001\n");
break;
}
}
while (choose != 5);
return 0;
}
You are reading the names into single chars, which is why xcode is giving you that message. You need to use an array of chars.
The simplest way to do this would be to just change your declarations to:
char name1[N], name2[N], name3[N], name4[N];
where N is the maximum length a name can be.
You would then change all the scanf to be like:
scanf(" %s", name1); // & removed
as name1 etc. now point to the start of the char arrays.
Note that this is inherently a risky thing to do, as it means that the buffer can be overflown by inputting a name that is too long.
You would be better off using fgets, as it allows you to specify the maximum length of the buffer:
fgets(name1, N, stdin);
Anything longer than N-1 characters will be discarded, rather than overflowing your buffer.
To print a char you use %c.
To print a string or array-of-char you use %s
You can use array of char char name[30] or char pointer char *name, and then use %s to print.
I have not looked into the logic of your program but make the following amendments and it should work fine.
#include <stdio.h>
//The maximum allowed name size, you can change it to whatever you want
#define MAX 10
void showthelastvalue () {
char name1[MAX], name2[MAX], name3[MAX], name4[MAX];
//You need character arrays here, simple char would store only a single byte
int value1a, value1b, value1c, value1d;
int value2a, value2b, value2c, value2d;
int value3a, value3b, value3c, value3d;
int value4a, value4b, value4c, value4d;
printf("insert student name 1 : ");
gets(name1); //scanf(" %s", &name1);
printf("insert student name 2 : ");
gets(name2); //scanf(" %s", &name2);
printf("insert student name 3 : ");
gets(name3); //scanf(" %s", &name3);
printf("insert student name 4 : ");
gets(name4); //scanf(" %s", &name4);
printf("Insert student value-1 %c\n", name1);
printf("value ke 1 : ");
scanf("%d", &value1a);
printf("value ke 2 : ");
scanf("%d", &value1b);
printf("value ke 3 : ");
scanf("%d", &value1c);
printf("value ke 4 : ");
scanf("%d", &value1d);
printf("Insert student value- 2 %c\n", name2);
printf("value ke 1 : ");
scanf("%d", &value2a);
printf("value ke 2 : ");
scanf("%d", &value2b);
printf("value ke 3 : ");
scanf("%d", &value2c);
printf("value ke 4 : ");
scanf("%d", &value2d);
printf("Insert student value- 3 %c\n", name3);
printf("value ke 1 : ");
scanf("%d", &value3a);
printf("value ke 2 : ");
scanf("%d", &value3b);
printf("value ke 3 : ");
scanf("%d", &value3c);
printf("value ke 4 : ");
scanf("%d", &value3d);
printf("Insert student value- 4 %c\n", name4);
printf("value ke 1 : ");
scanf("%d", &value4a);
printf("value ke 2 : ");
scanf("%d", &value4b);
printf("value ke 3 : ");
scanf("%d", &value4c);
printf("value ke 4 : ");
scanf("%d", &value4d);
printf("Jadi value dari 4 mahasiswa sbb : \n");
printf(" %s %d %d %d %d", name1, value1a, value1b, value1c, value1d);
printf(" %s %d %d %d %d", name2, value1a, value1b, value1c, value1d);
printf(" %s %d %d %d %d", name3, value1a, value1b, value1c, value1d);
printf(" %s %d %d %d %d", name4, value1a, value1b, value1c, value1d);
}
int main() {
int choose;
printf("Welcome!\n");
do {
printf("Choose anda :\n");
printf("1. Show the last value\n");
printf("2. Show Grade\n");
printf("3. Show the Table\n");
printf("4. Exit\n");
printf("What you will choose ? ");
scanf("%d", &choose);
switch (choose) {
case 1:
showthelastvalue();
break;
case 4:
printf("Thank you /001\n");
break;
}
}
while (choose != 5);
return 0;
}
Some explanation to what you did wrong: You had defined the variables as 'char' which would mean that your program is expecting a single byte. Also, use 'gets()' function instead of using scanf to store strings. '%s' only works with character arrays in printf.
You are using char name1,name2,name3,name4
replace it with
char name1[N],name2[N],name3[N],name4[N]
then use %s instead of %c.
and then use gets() function instead of using scanf() function.
then try it i hope it will run perfectly.