This is a homework problem. I have a C program that takes user input for a number of people's first names, last names, and ages. Right now it works and prints out the names to the console correctly, but it is not printing out the right ages, and I can't figure out what I'm doing wrong. Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int choice;
int i = 0;
int x,k,l;
fputs("How many people would you like to add? ", stdout);
scanf(" %d", &choice);
fflush(stdout);
int ch;
while((ch = getchar()) != EOF && ch != '\n');
if (ch == EOF)
{
}
char firstName[choice][20];
char lastName[choice][20];
int age[choice][3];
char first[20];
char last[20];
int a[3];
for (x = 0; x < choice; x++)
{
for (l = 0; l < 3; l++)
{
age[x][l] = 0;
a[l] = 0;
}
}
while(i < choice)
{
printf("Enter the first name of person ");
printf(" %d", i);
printf(": ");
fgets(first, 20, stdin);
for (k = 0; k < 20; k++)
{
firstName[i][k] = first[k];
}
i++;
}
i = 0;
while(i < choice)
{
printf("Enter the last name of person ");
printf(" %d", i);
printf(": ");
fgets(last, 20, stdin);
for (k = 0; k < 20; k++)
{
lastName[i][k] = last[k];
}
i++;
}
i = 0;
while(i < choice)
{
fputs("Enter the age of person ", stdout);
printf(" %d", i);
printf(": ");
scanf(" %d", &a);
fflush(stdout);
for (l = 0; l < 3; l++)
{
age[i][l] = a[l];
}
i++;
}
int sh;
while((sh = getchar()) != EOF && sh != '\n');
if (sh == EOF)
{
}
for (x = 0; x < choice; x++)
{
printf("First name ");
printf(": ");
printf("%s ", firstName[x]);
printf("\n");
printf("Last name ");
printf(": ");
printf("%s ", lastName[x]);
printf("\n");
printf("Age ");
printf(": ");
printf("%d ", &age[x]);
printf("\n");
}
return 0;
}
If you copy/paste this code it will run, but the age outputted will be incorrect. Can anyone tell me why this is? Thank you!
scanf(" %d", &a);
That should be:
scanf(" %d", &a[0]);
And the printf should be printf("%d", age[x][0]);
You want to read into the first element of the array, not the entire array. You want to print out the first element of the array, not the address of the array.
A better solution would probably be not to make age an array of 3 at all. Each person only has one age. The changes would be:
int age[choice];
int a;
scanf(" %d", &a);
age[choice] = a;
printf("%d ", age[x]);
Related
Repeat the Program for again Search Array Element.
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE];
int size, i, toSearch, found;
char repeat;
printf("Enter the size of an array\n");
scanf("%d", &size);
printf("Enter the array elements\n");
for (i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}
do{
printf("\nEnter element to search: ");
scanf("%d", &toSearch);
found = 0;
for(i=0; i<size; i++)
{
if(arr[i] == toSearch)
{
found = 1;
break;
}
}
if(found == 1)
{
printf("\n%d is found at position %d", toSearch, i + 1);
}
else
{
printf("\n%d is not found in the array \n", toSearch);
}
printf("\n \n \nPress Y to again Search Any Element in Array\n \nPress Any other Key to Exit the Program\n\n");
scanf(" %c \t",&repeat);
}
while(repeat == 'y' || repeat == 'Y' );
return 0;
}
I want to repeat my program when user give the input of Y || y otherwise it'll exit the program.
In this code i want to make an array then search the element after this show's the results and in last repeat the code from the search the element block.
I ran your code and everything seems to be working properly except for this line:
scanf(" %c \t",&repeat);
Remove the \t from the scanf and it should work properly. You don't want to scan for a tab character, just the 'Y' or 'y' character.
Also, your use of newlines is a bit unusual. Try putting newline characters at the end of your strings as opposed to the beginning.
Updated code:
#include <stdio.h>
#define MAX_SIZE 100
int main() {
int arr[MAX_SIZE];
int size, i, toSearch, found;
char repeat = ' ';
printf("Enter the size of an array\n");
scanf("%d", &size);
printf("Enter the array elements\n");
for (i = 0; i < size; i++)
scanf("%d", &arr[i]);
do{
printf("Enter element to search: \n");
scanf("%d", &toSearch);
found = 0;
for(i=0; i<size; i++) {
if(arr[i] == toSearch) {
found = 1;
break;
}
}
if(found == 1)
printf("%d is found at position %d\n", toSearch, i + 1);
else printf("%d is not found in the array\n", toSearch);
printf("Press Y to again Search Any Element in Array\nPress Any other Key to Exit the Program\n");
scanf(" %c",&repeat);
}
while(repeat == 'y' || repeat == 'Y' );
return 0;
}
Enclose the block of code you want to repeat in a while loop, something like
bool flag = false;
while(flag==true) {
//Code block
scanf("%c",&input)
if((input == 'y') || (input == 'Y')) {flag = true;}
else {flag = false;}
}
The first method that came to my mind :
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE];
int size, i, toSearch, found;
char repeat;
printf("Enter the size of an array\n");
scanf("%d", &size);
printf("Enter the array elements\n");
for (i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}
do
{
printf("\nEnter element to search: ");
scanf("%d", &toSearch);
found = 0;
for (i = 0; i < size; i++)
{
if (arr[i] == toSearch)
{
found = 1;
break;
}
}
if (found == 1)
{
printf("\n%d is found at position %d", toSearch, i + 1);
}
else
{
printf("\n%d is not found in the array \n", toSearch);
}
printf("\n \n \nPress Y to again Search Any Element in Array\n \nPress Any other Key to Exit the Program\n\n");
repeat = getchar();
repeat = getchar();
if(repeat == 'y' || repeat == 'Y') {
continue;
}
else {
break;
}
} while (1);
return 0;
}
I've got a task to input some information of some student and export it in the form of a report
#include <stdio.h>
#define MAX 1000
typedef struct
{
char id[6];
char name[80];
float grade;
char classassessment;
} exam;
void clear_buffer()
{
int ch;
while ((ch = getchar()) != '\n' && ch != EOF)
;
}
char assess(float a)
{
if (9 <= a && a <= 10)
return 'A';
else if (8 <= a && a < 9)
return 'B';
else if (6.5 <= a && a < 8)
return 'C';
else
return 'D';
}
int main()
{
char result[MAX];
exam student[MAX];
int n, i=0;
printf("The number of students: ");
scanf("%d", &n);
clear_buffer();
for (i = 0; i < n; i++)
{
printf("Enter student %d ID: ", i + 1);
scanf("%s", student[i].id);
clear_buffer();
printf("Enter this student name: ");
gets(student[i].name);
printf("Enter this student's grade: ");
scanf("%f", &student[i].grade);
clear_buffer();
result[i] = assess(student[i].grade);
}
for(i=0; i<n; i++)
{
printf("%d\t%s\t\t\t%s\t\t%c\n", i+1, student[i].id, student[i].name, result[i]);
}
return 0;
}
After compiling it, you can see that student[i].id has both ID and name while student[i].name still has names of the students that have just been inputted. I can not explain why it has this error.
i was new with array for C programming. Anybody can help me? I want to print all array that saved from the assignment. Please help!
#include <stdio.h>
int main(){
const char *a[3];
char s[100];
int data, i=0;
back:
printf("Please insert name: ");
scanf(" %s", &s);
a[i] = s;
printf("Do you want to add data?: ");
scanf("%d", &data);
if (data==1){
i++;
goto back;}
else{
printf("%s", a[0] , a[1] , a[2]);}
return 0;
}
My suggestion would be never to use goto statement in C , it really mess up the readability of your source code. Also, go through this link, Why goto statement is not generally used.
Apart from this, you code can be simple re-written like this using for-loop statement.
#include <stdio.h>
int main(){
char *a[3];
char s[100];
int data = 1, i;
for(i=0; i<3 && data == 1; i++) {
printf("Please insert name: ");
scanf(" %s", &s);
a[i] = s;
printf("Do you want to add data?: ");
scanf("%d", &data);
}
for(i=0; i<3; i++)
printf("%s", a[i]);
return 0;
}
#include <stdio.h>
int main()
{
const char *a[3];
char s[100];
int data, i = 0, j;
back: printf("Please insert name: ");
scanf(" %s", &s);
a[i] = s;
printf("Do you want to add data?: ");
scanf("%d", &data);
i++;
if (data == 1)
{
goto back;
}
else
{
for (j = 0; j < i; j = j + 1)
printf("%s ", a[j]);
}
return 0;
}
So, I managed to wrangle some code to get part of my program working. My program has to have a prompt to enter grades (done), repeat in a loop until broken (doneish), and print results of each grade entered. The last part is where I am stuck. I can't seem to find a good way to get any grade I entered to print at the end. I just want a "you entered ###, ###, ###," or something similar, but it can be up to 100 numbers. Below is what I have so far
#include <stdio.h>
#define MAX_ARRAY_SIZE 100
int main(void) {
int grade[MAX_ARRAY_SIZE];
int entryCount = 0;
char continueResponse;
printf("Enter an grade of between 1 and 100. \n");
printf("Enter a maximum of %d grades. \n", MAX_ARRAY_SIZE);
int i;
for(i = 0; i < MAX_ARRAY_SIZE; i++) {
printf("Enter grade: ");
scanf("%d", &grade[i]);
printf("Continue? (y/n): ");
scanf(" %c", &continueResponse);
entryCount++;
if(continueResponse == 'n' || continueResponse == 'N') {
printf(" == End of Data Entry ==\n\n");
break;
}
}
return 0;
}
Keep in mind this is third week of doing this, so I know next to nothing. If there's a "why did you do this like this", the answer is because that's how I've done it before and it works. I appreciate any input!
After your dataentry loop:
for(i = 0; i < MAX_ARRAY_SIZE; i++) {
...
}
You just have to add a second loop:
for(i = 0; i < entryCount; i++) {
printf ("%d ", grade[i]);
}
You've recorded entryCount entries to the array, numbered 0 ... entryCount - 1; you'd use another for loop to print them. For nicer formatting we do not print ", " after the last number:
printf("You've entered ");
for (i = 0; i < entryCount; i++) {
if (i == entryCount - 1) {
printf("%d", grade[i]);
}
else {
printf("%d, ", grade[i]);
}
}
printf("\n");
what I understood about your problem the following code would work:
#include <stdio.h>
#define MAX_ARRAY_SIZE 100
int main(void) {
int grade[MAX_ARRAY_SIZE];
int entryCount = 0;
char continueResponse;
printf("Enter an grade of between 1 and 100. \n");
printf("Enter a maximum of %d grades. \n", MAX_ARRAY_SIZE);
int i;
for(i = 0; i < MAX_ARRAY_SIZE; i++) {
printf("Enter grade: ");
scanf("%d", &grade[i]);
printf("Continue? (y/n): ");
scanf(" %c", &continueResponse);
entryCount++;
printf("you entred:\n");
for(int j=0;j<entryCount;j++)
{
printf("%d ",grade[j]);
}
if(continueResponse == 'n' || continueResponse == 'N') {
printf(" == End of Data Entry ==\n\n");
break;
}
}
return 0;
}
I have just learnt about arrays. I was trying to create a database program using arrays. It's a very basic program.
#include<stdio.h>
#define N 1 //number of entries needed
int main()
{
int i, k = 1, l = 1, w, x = 0, y = 0;
int rollnum[N], hsc[N], cet[N], a[N], b[N];
char name[100], city[100], c;
for(i = 0; i < N; i++)
{
printf("%d.\n", (i+1));
printf("Enter first name : ");
do
{
c = getchar();
if(c != '\n')
{
name[k] = c;
k++;
}
}
while(c != '\n');
a[i] = k;
k++;
printf("\n");
printf("Enter roll number : ");
scanf("%d", &rollnum[i]);
printf("\n");
getchar();
printf("Enter city : ");
do
{
c = getchar();
if(c != '\n')
{
city[l] = c;
l++;
}
}
while(c != '\n');
b[i] = l;
l++;
printf("\n");
printf("Enter HSC percentage : ");
scanf("%d", &hsc[i]);
printf("\n");
printf("Enter CET marks : ");
scanf("%d", &cet[i]);
printf("\n");
getchar();
}
printf("\n\n\n");
k = 1;
l = 1;
for(i = 0; i < N; i++)
{
printf("Entry %d\n", (i+1));
printf("Student Name : ");
x = (a[i] - x);
for(w = 0; w < x; w++ && k++)
putchar(name[k]);
putchar('\n');
printf("Roll number : %d", rollnum[i]);
printf("\n");
printf("City : ");
y = (b[i] - y);
for(w = 0; w < y; w++ && l++)
putchar(city[l]);
putchar('\n');
printf("Marks : \n");
printf("\t");
printf("HSC : %d ", hsc[i]);
printf("\t");
printf("CET : %d / 200", cet[i]);
printf("\n\n\n");
}
return 0;
}
The program is not functioning the way I want it to! When I enter a name, the first letter is being printed out twice, same is the case with city! If I put 2 entries by modifying my 'N' , I'm getting the first letters of name and address(of second entry) as garbage values . I don't think there is any error in my logic, because i tried doing it manually in my notebook and I did not find any fault in it.
Can anyone help me find the mistake? I know the program might not at all be good and efficient, but I'm just trying out stuff that I've learnt!
You have made some mistakes initializing some variables. Also you don't have to read a string one character at a time. You can use scanf("%s", someString) to read a whole string.
And here is a working code that looks a lot more cleaner:
#include<stdio.h>
#define N 2
int main()
{
int rollnum[N], hsc[N], cet[N], i;
char name[100][100], city[100][100];
for(i = 0; i < N; i++)
{
printf("%d.\nEnter first name : ", (i+1));
scanf("%s", name[i]);
printf("\nEnter roll number : ");
scanf("%d", &rollnum[i]);
printf("\nEnter city : ");
scanf("%s", city[i]);
printf("\nEnter HSC percentage : ");
scanf("%d", &hsc[i]);
printf("\nEnter CET marks : ");
scanf("%d", &cet[i]);
printf("\n");
}
printf("\n\n\n");
for(i = 0; i < N; i++)
{
printf("Entry %d\nStudent Name : %s\nRoll number : %d\nCity : %s\nMarks : \n\tHSC : %d \tCET : %d / 200\n\n\n", (i+1), name[i], rollnum[i], city[i], hsc[i], cet[i]);
}
return 0;
}
It works for multiple entries too.
In order to make your program work you have to replace this:
x = (a[i] - x);
for(w = 0; w < x; w++ && k++)
putchar(name[k]);
putchar('\n');
printf("Roll number : %d", rollnum[i]);
printf("\n");
printf("City : ");
y = (b[i] - y);
for(w = 0; w < y; w++ && l++)
putchar(city[l]);
with this:
if (i == 0)
x = a[i]-1;
else
x = a[i] - a[i-1];
for(w = 0; w < x; w++)
putchar(name[k++]);
putchar('\n');
printf("Roll number : %d", rollnum[i]);
printf("\n");
printf("City : ");
if (i == 0)
y = b[i]-1;
else
y = b[i] - b[i-1];
for(w = 0; w < y; w++)
putchar(city[l++]);
the problem was that you didn't calculate the length of the words correctly, and also another error that I still can't explain but I removed that to.