C code that displays values inputted by the user - arrays

This code is designed to print out the values inputted by the user but the code is printing some invalid results
#include<stdio.h>
int main(){
int age;
char array[][50] = {"name",
"colour", "country"};
printf("What is your
name\n");
scanf("%s", array[1]);
printf("How old are
you\n");
scanf("%d", &age);
printf("What is your
favorite colour\n");
scanf("%s", array[2]);
printf("Which country are
youu from\n");
scanf("%s", array[3]);
printf("Information
collected about you is\n");
printf("Name: %s\n Age:
%d\n Colour: %s\n Colour:
%s\n Country: %s\n",
array[1], age, array[2],
array[3]);
}

You have five printings, in the final line.
printf ("Name: %s\n Age: %d\n Colour: %s\n Colour: %s\n Country: %s\n", array[1], age, array[2], array[3]);
That is, you have 2 color entries, and you only want 1.
The compiler does not know where to look for the Country %s part and then generates a segmentation fault. Segmentation faults happen when you dereference an invalid pointer.

Related

Can anyone tell me what's wrong in my code given below?

I want to take input from user using structure. So I'm using the code like below. But it's not printing the values which I'm entering. Can anyone help me?
#include <stdio.h>
int main()
{
struct book
{
char name;
float price;
int pages;
};
struct book b1, b2;
printf("Enter the Name, Price and Pages of Book 1: ");
scanf("%c %f %d", &b1.name, &b1.price, &b1.pages);
printf("Enter the Name, Price and Pages of Book 2: ");
scanf("%c %f %d", &b2.name, &b2.price, &b2.pages);
printf("Here is the data you've entered: \n");
printf("Name: %c Price: %f Pages: %d\n", b1.name, b1.price, b1.pages);
printf("Name: %c Price: %f Pages: %d\n", b2.name, b2.price, b2.pages);
return 0;
}
But I'm not getting the output as desired. My
Output Image
Your question is similar to this one: scanf() leaves the newline character in the buffer
And can be corrected like this:
#include <stdio.h>
int main()
{
struct book
{
char name;
float price;
int pages;
};
char buffer[255];
struct book b1, b2;
printf("Enter the Name, Price and Pages of Book 1: ");
scanf(" %c %f %d", &b1.name, &b1.price, &b1.pages);
printf("Enter the Name, Price and Pages of Book 2: ");
scanf(" %c %f %d", &b2.name, &b2.price, &b2.pages);
printf("Here is the data you've entered: \n");
printf("Name: %c Price: %f Pages: %d\n", b1.name, b1.price, b1.pages);
printf("Name: %c Price: %f Pages: %d\n", b2.name, b2.price, b2.pages);
return 0;
}
Note the leading space before the %c input.

printf not printing integer stored in variable

For some weird reason when I input the values in the marks section, the printed values are all 0, please help, I tried running a smaller version of the code and it worked I don't know why this doesn't.
here is the code:
Stack overflow keeps giving me an error since apparently most of my question is code so pls ignore this
#include <stdio.h>
#include <stdlib.h>
int main()
{
int marksComm;
char gradeComm[1];
int marksEco;
char gradeEco[1];
int marksCompsys;
char gradeCompsys[1];
int marksProg;
char gradeProg[1];
int marksDis;
char gradeDis[1];
int marksLab;
char gradeLab[1];
int marksPhy;
char gradePhy[1];
printf("CCS001\nMarks> ");
scanf("%d", &marksComm);
printf("Grade> ");
scanf("%s", &gradeComm);
printf("\nCCS009\nMarks> ");
scanf("%d", &marksEco);
printf("Grade> ");
scanf("%s", &gradeEco);
printf("\nCSC111\nMarks> ");
scanf("%d", &marksCompsys);
printf("Grade> ");
scanf("%s", &gradeCompsys);
printf("\nCSC112\nMarks> ");
scanf("%d", &marksProg);
printf("Grade> ");
scanf("%s", &gradeProg);
printf("\nCSC113\nMarks> ");
scanf("%d", &marksDis);
printf("Grade> ");
scanf("%s", &gradeDis);
printf("\nCSC126\nMarks> ");
scanf("%d", &marksPhy);
printf("Grade> ");
scanf("%s", &gradePhy);
printf("\nCSC115\nMarks> ");
scanf("%d", &marksLab);
printf("Grade> ");
scanf("%s", &gradeLab);
printf("\nCourse Code Marks Grade\n");
printf("CCS001 %d %s \n", marksComm, gradeComm);
printf("CCS009 %d %s \n", marksEco, gradeEco);
printf("CSC111 %d %s \n", marksCompsys, gradeCompsys);
printf("CSC112 %d %s \n", marksProg, gradeProg);
printf("CSC113 %d %s \n", marksDis, gradeDis);
printf("CSC115 %d %s \n", marksLab, gradeLab);
printf("CSC126 %d %s \n", marksPhy, gradePhy);
}
Your arrays are two small. C strings are NUL-terminated, so you need a char for the letter, and a char for the NUL. That's two char. But you reserve space for one.

C - Scanf not getting correct info

Why is it when I run my code I get this result:
Name: Zeref
Age: 20
Float: 20.11
-----
Name: Zeref
Age: 1072324272
Dec: 0.000000
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char name[524288];
int age[524288];
float dec[524288];
printf("Name: ");
scanf("%s", name);
printf("Age: ");
scanf("%d", age);
printf("Float: ");
scanf("%f", dec);
printf("-----\n");
printf("Name: %s\n", name);
printf("Age: %d\n", age);
printf("Dec: %f\n", dec);
return 0;
}
it isn't taking in what I say correctly, why?
I want it to print out exactly what I write but using age as an int, dec as a float and name as a string. Only name works
This is just printing the address of the array age as a decimal integer (actually it introduces undefined behavior);
printf("Age: %d\n", age);
Try this
printf("Age: %d\n", age[0]);

Printing structures using loop

How to print structure contents using for-loop? If pointers, how to assign pointer to structure? I have attached code that I made that makes a structure using data that user inputs, and prints them out in an orderly fashion. My problem is that you have to write a lot of printf() and gets_s() statements to receive and print input. I feel like it would be easier to do this using a for-loop. I tried to make a for-loop as you can see in the code using pointers, but it doesn't compile, and I'm pretty sure that it is the wrong way to assign pointers to structures.
TL;DR: How to print structure contents using for-loop? If pointers, how to assign pointer to structure?
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <conio.h>
#define maxName 50
#define maxNation 50
#define maxAge 100
struct astronaut
{
char firstName[maxName];
char lastName[maxName];
char nation[maxNation];
int age = 0;
char missionName[maxAge];
int missionYear[50];
};
int main()
{
int i = 0;
struct astronaut candidate1;
struct astronaut candidate2;
struct astronaut candidate3;
struct astronaut mission1;
struct astronaut mission2;
struct astronaut mission3;
printf("Please enter the first name of the candidate: ");
gets_s(candidate1.firstName);
printf("Please enter the last name of the candidate: ");
gets_s(candidate1.lastName);
printf("Please enter the nationality of the candidate: ");
gets_s(candidate1.nation);
printf("Please enter the age of the candidate: ");
scanf("%d", &candidate1.age);
printf("Please enter the first name of the candidate: ");
gets_s(candidate2.firstName);
printf("Please enter the last name of the candidate: ");
gets_s(candidate2.lastName);
printf("Please enter the nationality of the candidate: ");
gets_s(candidate2.nation);
printf("Please enter the age of the candidate: ");
scanf("%d", &candidate2.age);
printf("Please enter the first name of the candidate: ");
gets_s(candidate3.firstName);
printf("Please enter the last name of the candidate: ");
gets_s(candidate3.lastName);
printf("Please enter the nationality of the candidate: ");
gets_s(candidate3.nation);
printf("Please enter the age of the candidate: ");
scanf("%d", &candidate3.age);
printf("Enter a Mission Name: ");
gets_s(mission1.missionName);
printf("Enter the year the mission was conducted: ");
scanf("%d", &mission1.missionYear);
printf("Enter a Mission Name: ");
gets_s(mission2.missionName);
printf("Enter the year the mission was conducted: ");
scanf("%d", &mission2.missionYear);
printf("Enter a Mission Name: ");
gets_s(mission3.missionName);
printf("Enter the year the mission was conducted: ");
scanf("%d", &mission3.missionYear);
struct astronaut *ptr;
struct candidate *ptr;
// printf("\n\tAstronaut Candidate Database\n");
// printf("Name: %s %s \t Age: %d \t Nationality: %s\n", candidate1.firstName, candidate1.lastName, candidate1.age, candidate1.nation);
// printf("Name: %s %s \t Age: %d \t Nationality: %s\n", candidate2.firstName, candidate2.lastName, candidate2.age, candidate2.nation);
// printf("Name: %s %s \t Age: %d \t Nationality: %s\n", candidate3.firstName, candidate3.lastName, candidate3.age, candidate3.nation);
// printf("\n\tAstronaut Mission Database\n");
// printf("Mission Name: %s \t Year: %d\n", mission1.missionName, mission1.missionYear);
// printf("Mission Name: %s \t Year: %d\n", mission2.missionName, mission2.missionYear);
// printf("Mission Name: %s \t Year: %d\n", mission3.missionName, mission3.missionYear);
for (int index = 0; index < 5; index++)
{
printf("Name: %s %s \t Age: %d \t Nationality: %s\n", *ptr.firstName, *ptr.lastName, *ptr.age, *ptr.nation);
ptr++;
}
_getch();
return 0;
}
I think you need 2 structs. An astronaut and a mission aren't the same thing. As it is now, you have several unused fields depending on whether you're entering data for an astronaut or a mission. You could do something like this:
#include <stdio.h>
#define maxName 50
#define maxNation 50
#define maxAge 100
struct astronaut
{
char firstName[maxName];
char lastName[maxName];
char nation[maxNation];
int age;
};
struct mission
{
char missionName[maxAge];
int missionYear; // why do you have an array of 50 ints for the missionYear?
};
void enter_candidate_data(struct candidate* cand)
{
// not famliar with gets_s .. I would use fgets here, you can read the manpage on that if you desire
printf("Please enter the first name of the candidate: ");
gets_s(cand->firstName);
printf("Please enter the last name of the candidate: ");
gets_s(cand->lastName);
printf("Please enter the nationality of the candidate: ");
gets_s(cand->nation);
printf("Please enter the age of the candidate: ");
scanf("%d", &(cand->age));
}
void enter_mission_data(struct mission* mis)
{
printf("Enter a Mission Name: ");
gets_s(mis->missionName);
printf("Enter the year the mission was conducted: ");
scanf("%d", &(mis->missionYear));
}
int main()
{
int i;
struct astronaut candidates[3];
struct mission missions[3];
for (i=0; i<3; i++)
{
enter_candidate_data(&(candidates[i]));
// you can put this in a separate loop if you want to enter all
// candidate data first
enter_mission_data(&(missions[i]));
}
// you could also write functions to print the data instead, depends on
// how you want it all presented
for (int i=0; i<3; i++)
{
printf("Name: %s %s \t Age: %d \t Nationality: %s\n",
candidates[i].firstName, candidates[i].lastName, candidates[i].nation);
printf("Mission Name: %s, year %d\n", missions[i].missionName,
missions[i].missionYear);
}
return 0;
}
You may want a number of missions (or even just one) associated with a particular astronaut. If so, I would define the data structures like this
#define MAX_ASTRONAUT_MISSIONS 20
struct mission
{
char missionName[maxAge];
int missionYear;
};
struct astronaut
{
char firstName[maxName];
char lastName[maxName];
char nation[maxNation];
int age;
struct mission missions[MAX_ASTRONAUT_MISSIONS];
};
This will allow you to associate MAX_ASTRONAUT_MISSION missions with each astronaut. Or, more realistically, a single mission could be associated with several astronauts. In that case, you may want data structures more like this
struct mission
{
char missionName[maxAge];
int missionYear;
};
struct astronaut
{
char firstName[maxName];
char lastName[maxName];
char nation[maxNation];
int age;
// using a pointer to missions will allow you to create one mission, and
// all the astronauts on that mission could get a pointer to it,
// designating they were all on that singular mission.
struct mission* missions[MAX_ASTRONAUT_MISSIONS];
};

C program, designing a check

I'm writing a program that basically just takes a bunch of user input and Outputs it in the form of a check. My problem is that I'm entering 2 sentences and while the second one is fine it completly skips the first one! and the user last name is working but the first name just outputs "z" it is the weirdest thing and my teachers philosophy is figure it out yourself. So can anyone possibly help me?? Here is my code...
#include<stdio.h>
#include<string.h>
int main()
{
char date[8];
int checkNum;
char payeeFirst[10];
char payeeLast[10];
double amount;
char memo[50];
char wordAmount[100];
printf("Please enter the date: ");
scanf("%s", &date);
printf("Please enter the check number: ");
scanf("%d", &checkNum);
printf("Please enter the payee First name: ");
scanf("%s", &payeeFirst);
printf("Please enter payee last name: ");
scanf("%s", &payeeLast);
printf("Please enter amount: ");
scanf("%d", &amount);
printf("Please enter amount in words: ");
fgets (wordAmount, sizeof(wordAmount)-1, stdin);
printf("Please enter memo: ");
fgets (memo, sizeof(memo)-1, stdin);
printf(" \n");
printf("Date: %s .\n", date);
printf("Check Number: %d .\n", checkNum);
printf("Payee: [%s] [%s] .\n", payeeFirst, payeeLast);
printf("Amount: %d .\n", amount);
printf(" Check %d \n", checkNum);
printf(" Date: %s \n", date);
printf("Pay to the\n");
printf("Order of %s %s $%d \n", payeeFirst, payeeLast, amount);
printf("%s", wordAmount);
printf(" \n");
printf(" \n");
printf("Memo: %s \n", memo);
return 0;
}
Your scanf calls all leave the '\n' in the stream (the next scanf will ignore it though).
The first fgets reads an empty string (well ... a string containing a single '\n').
Try reading the numbers with fgets too (to a temporary buffer) and sscanf from the buffer to the correct variable. This way all the '\n' will be accounted for.

Resources