C Print Data from loop after loop ends - c

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;
}

Related

How do I add a do-while loop in this program so that if the input is less than 4 it doesn't print anything

#include <stdio.h>
int main () {
int row, i, j;
printf("Enter a number: ");
scanf("%d", &row);
for (i=1; i<=row; i++) {
for (j=1; j<=row; j++) {
if (i==1 || i==row || i+j==row+1) {
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
printf("\n\n");
}
return 0;
}
The program prints out the letter "Z" out of stars.
I have to add a do-while loop in this. (This is for school, you can clearly see that I'm a begginer.)
Perhaps this is what you want:
do {
printf("Enter a number: ");
scanf("%d", &row);
if (row < 4) puts("Try again");
} while (row < 4);

Repeat the Program for again Search Array Element

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;
}

Getting error "passing argument 2 of ‘strcmp’ makes pointer from integer without a cast" comparing two strings

Im a student, and im trying to complete this program, which his objective is:
Print the data of the product that has the same name as the requested name in input
Everytime, when i try to compile the program, this warning appears:
warning: passing argument 2 of ‘strcmp’ makes pointer from integer without a cast
#include <stdio.h>
#include <string.h>
#define N 100
#define M 8
int main (void) {
int n;
char name[N][M];
char code[N][M];
int price[N];
char searchname;
int i;
int selection;
int search, x;
do {
printf("Insert how many products to register: ");
scanf("%d", &n);
} while(n > N);
for(i = 0; i < n; i++) {
printf("Insert the name of the product n%d: ", i + 1);
scanf("%s", name[i]);
printf("Insert the code of the product registered: ");
scanf("%s", code[i]);
printf("Insert the price of the product registered: ");
scanf("%d", &price[i]);
}
do {
printf("Choose one of the following options:\n\n");
printf("1) Print name and price of the searched product (code)\n");
printf("2) Print the product that has the same name as the name inserted\n");
printf("0) Close the program\n\n");
printf("Type the number of the option: ");
scanf("%d", &selection);
switch(selezione) {
case 1:
x = 0;
printf("Insert the code of the product: ");
scanf("%d", &search);
for(i = 0; i < n; i++) {
if(code[i] == search) {
printf("name: %s | price: %d\n\n", name[i], price[i]);
x = 1;
}
}
if(x == 0) {
printf("The searched code doesnt exist\n\n");
}
break;
case 2:
x = 0;
printf("Insert the name to search: ");
scanf("%s", searchname);
for(i = 0; i < n; i++) {
while(strcmp(name[i], searchname) == 0) {
printf("Product number %d | Code: %s | Price: %d\n\n", i + 1, code[i], price[i]);
x++;
}
}
if(x == 0) {
printf("There are no products with this name\n\n");
}
break;
case 0:
break;
default:
printf("This option doesnt exist");
break;
}
} while(selection!= 0);
return 0;
}
I searched around stack overflow for a while, and the posts that mention this problem weren't helpful.
What im trying to do, is compare the string name[i] with searchname which is a string too.
Am i missing something here?
What im trying to do, is compare the string name[i] with searchname which is a string too.
char searchname; - doesn't look like string.
– Eugene Sh.
You could try char searchname[100]; …
– Weather Vane

Use of strings in structs in C language

I've been studying C for almost three months now and I neved had much trouble on the way. However, I have this task to create a program that will arrange an array of products (chosen by the user), either by their price or their available quantity.
I had to use a struct called Product to do so. Problem is, when I enter on any of the functions (arrangePrice or arrangeQuantity), the console prints the "What is the name of product?" printf command and IMMEDIATELY prints out the "What is the price of the product?" printf command. It simply seems to ignore the scanf function between those commands that would let the user write the name of the product on a string. Why is that happening???
Here is the entire code:
#include <stdio.h>
#include <string.h>
struct Product
{
char name[80];
double price;
int quantity;
};
void arrangePrice(struct Product vet[], int n)
{
int i, j, auxQuant;
double aux, auxprice;
char auxname[80];
for (i = 0; i < n; i++)
{
printf("What is the name of the product?\n");
scanf("%[^\n]", vet[i].name);
printf("What is the price of the product?\n");
scanf("%lf", &vet[i].price);
printf("What is the available quantity of the product?\n");
scanf("%d", &vet[i].quantity);
}
printf("\n\nArranging by price:\n\n");
for (j = 0; j < n; j++)
{
aux = vet[j].price;
for (i = j + 1; i < n; i++)
{
if (vet[i].price < aux)
{
auxprice = vet[i].price;
strcpy(auxname, vet[i].name);
auxQuant = vet[i].quantity;
vet[i] = vet[j];
strcpy(vet[j].name, auxname);
vet[j].price = auxprice;
vet[j].quantity = auxQuant;
}
}
}
for (i = 0; i < n; i++)
{
printf("%[^\n] -> %.2lf\n", vet[i].name, vet[i].price);
}
}
void arrangeQuant(struct Product vet[], int n)
{
int i, j, aux, auxQuant;
double auxprice;
char auxname[80];
for (i = 0; i < n; i++)
{
printf("What is the name of the product?\n");
scanf("%[^\n]", vet[i].name);
printf("What is the price of the product?\n");
scanf("%lf", &vet[i].price);
printf("What is the available quantity of the product?\n");
scanf("%d", &vet[i].quantity);
}
printf("\n\nArranging by available quantity:\n\n");
for (j = 0; j < n; j++)
{
aux = vet[j].quantity;
for (i = j + 1; i < n; i++)
{
if (vet[i].quantity < aux)
{
auxprice = vet[i].price;
strcpy(auxname, vet[i].name);
auxQuant = vet[i].quantity;
vet[i] = vet[j];
strcpy(vet[j].name, auxname);
vet[j].price = auxprice;
vet[j].quantity = auxQuant;
}
}
}
for (i = 0; i < n; i++)
{
printf("%[^\n] -> %d\n", vet[i].name, vet[i].quantity);
}
}
int main()
{
struct Product prod[51];
int n;
int choice;
printf("How many products will be added? (Maximum of 50)\n");
scanf("%d", &n);
while (n < 1 || n > 50)
{
printf("Invalid value. Try again.\n");
scanf("%d", &n);
}
printf("Do you want to arrange by price or by available quantity? (0 or 1)\n");
scanf("%d", &choice);
if (choice == 0) arrangePrice(prod, n);
else if (choice == 1) arrangeQuant(prod, n);
else printf("Invalid value.\n");
return 0;
}
I must say that I actually still don't know if the code is right or not, since I couldn't type the name of the products. Thanks for any help!
Your scanf calls are leaving newline characters in the input buffer, and those newlines are being read on subsequent calls.
You need to leave a space at the start of each scanf pattern to consume any newlines that may be left over.
Also, %[^\n] is not a valid format specifier for printf. Use %s instead.
Example:
printf("What is the name of the product?\n");
scanf(" %s", vet[i].name); // use %s for strings
printf("What is the price of the product?\n");
scanf(" %lf", &vet[i].price);
printf("What is the available quantity of the product?\n");
scanf(" %d", &vet[i].quantity);
And:
printf("How many products will be added? (Maximum of 50)\n");
scanf(" %d", &n);
while (n < 1 || n > 50)
{
printf("Invalid value. Try again.\n");
scanf(" %d", &n);
}
printf("Do you want to arrange by price or by available quantity? (0 or 1)\n");
scanf(" %d", &choice);
struct product vet[] is a pointer. It doesn't actually point to an array unless you allocate such an array before you call arrangePrice. For example:
struct Product vet_array[ 2 ];
arrangePrice( vet_array, 2 );
Alternatively you could call malloc but just to get this started to work, allocate a local on the stack with a fixed number of elements.

Reading in and recording a number in C

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]);

Resources