Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I wrote a c program for a shopinng list below, the program should be able to run

ID: 3890460 • Letter: I

Question

I wrote a c program for a shopinng list below, the program should be able to run on ocelot,

#include <stdio.h>
#include<string.h>
#include <stdlib.h>
struct ShoppingList
{
int itemNum;
char description[40];
double price;
int count;
double extendedPrice;
};

int main(void) {

struct ShoppingList list[5];
//Item Num 1
list[0].itemNum = 345;
strcpy(list[0].description, "Bookshelf");
list[0].price = 78.50;
list[0].count = 4;

//Item num 2
list[1].itemNum = 7474;
strcpy(list[1].description ,"Pen");
list[1].price = 2.99;
list[1].count = 100;

//Item num 3
list[2].itemNum = 987;
strcpy(list[2].description,"Chair");
list[2].price = 129.99;
list[2].count = 6;

//Item num 4
list[3].itemNum = 2342;
strcpy(list[3].description ,"Camera");
list[3].price = 1295.40;
list[3].count = 3;

//Item num 5
list[4].itemNum = 2731;
strcpy(list[4].description, "Table");
list[4].price = 185.40;
list[4].count = 2;
printf(" Item Num Description Price Count Extended Price ");
for(int i=0; i<5; i++)
{
int counter = 0;
char buffer[10]; // Increased buffer size to 10
snprintf(buffer, 10, "%d", list[i].itemNum);
counter = strlen(buffer);
char zeros[7] = "";
while(counter<6)
{
strcat(zeros,"0");
counter++;
}
sprintf(buffer, "%s%d", zeros, list[i].itemNum); // copying the whole item number(with the zeros) into buffer to make it string
printf("%8s%14s ", buffer, list[i].description); // Printing two consecutive strings buffer and description at once
sprintf(buffer, "$%.2f", list[i].price); // Converting price into string
printf("%10s ", buffer); // Printing string price in a formatted way i.e. with spaces for extra positions
sprintf(buffer, "%d", list[i].count); // converting count to string
printf("%5s ", buffer); // Printing string count with format to align
sprintf(buffer, "$%.2f", list[i].price*list[i].count); // converting extended price to string
printf("%14s ", buffer); // Printing extended price
}
return 0;
}

it runs when i run it on everything else but its supposed to be able to run on ocelot, when i try to run it on ocelot im getting these errors

shopping.c: In function ‘main’:
shopping.c:60: error: ‘for’ loop initial declarations are only allowed in C99 mode
shopping.c:60: note: use option -std=c99 or -std=gnu99 to compile your code

could somebody please help me fix these errors so that the code will compile in ocelot. please help.

Explanation / Answer

Note:- You are getting that error because you are declaring loop variable inside the loop. I changed it to declare outside the loop. Hope this should work.

#include <stdio.h>

#include<string.h>
#include <stdlib.h>
struct ShoppingList
{
int itemNum;
char description[40];
double price;
int count;
double extendedPrice;
};
int main(void) {
struct ShoppingList list[5];
//Item Num 1
list[0].itemNum = 345;
strcpy(list[0].description, "Bookshelf");
list[0].price = 78.50;
list[0].count = 4;
//Item num 2
list[1].itemNum = 7474;
strcpy(list[1].description ,"Pen");
list[1].price = 2.99;
list[1].count = 100;
//Item num 3
list[2].itemNum = 987;
strcpy(list[2].description,"Chair");
list[2].price = 129.99;
list[2].count = 6;
//Item num 4
list[3].itemNum = 2342;
strcpy(list[3].description ,"Camera");
list[3].price = 1295.40;
list[3].count = 3;
//Item num 5
list[4].itemNum = 2731;
strcpy(list[4].description, "Table");
list[4].price = 185.40;
list[4].count = 2;
printf(" Item Num Description Price Count Extended Price ");

//changed here to declare loop variable outside loop
int i;
for(i=0; i<5; i++)
{
int counter = 0;
char buffer[10]; // Increased buffer size to 10
snprintf(buffer, 10, "%d", list[i].itemNum);
counter = strlen(buffer);
char zeros[7] = "";
while(counter<6)
{
strcat(zeros,"0");
counter++;
}
sprintf(buffer, "%s%d", zeros, list[i].itemNum); // copying the whole item number(with the zeros) into buffer to make it string
printf("%8s%14s ", buffer, list[i].description); // Printing two consecutive strings buffer and description at once
sprintf(buffer, "$%.2f", list[i].price); // Converting price into string
printf("%10s ", buffer); // Printing string price in a formatted way i.e. with spaces for extra positions
sprintf(buffer, "%d", list[i].count); // converting count to string
printf("%5s ", buffer); // Printing string count with format to align
sprintf(buffer, "$%.2f", list[i].price*list[i].count); // converting extended price to string
printf("%14s ", buffer); // Printing extended price
}
return 0;
}