Having lots of issues with my code, and have had people try explaining it to me but I still can't understand what I'm doing wrong... it all seems right in my head. As far as I understand, my char is not scanning correctly. I also have issues with returning values, and my "int * differs in levels of indirection from int". How can I fix my code? I'm completely lost.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define PAUSE system("pause")
#define CLS system("cls")
#define FLUSH flush()
// PROTOTYPE FUNCTIONS
void displayMenu();
void flush();
char getUserChoice();
int newSale(int *price, char *firstTime, char *veteran, char *student, char *lastDay);
int outputPrice(int carSales[], int *carsSold, int *avgCarPrice, int *totalRevenue);
// MAIN
main() {
int carSales[500] = { 0 };
int price = 0, avgCarPrice = 0, totalRevenue = 0, carsSold = 0;
char firstTime = 'n', veteran = 'n', student = 'n', lastDay = 'n';
char userSelection;
do {
userSelection = getUserChoice();
switch (userSelection) {
case 'A': // ENTER A NEW SALE
newSale(&price, &firstTime, &veteran, &student, &lastDay); // call function to ask questions
printf("\nPRICE OF CAR: %i\n", price);
carsSold++; // will add to +1 to the amount of cars sold
carSales[carsSold] = price; // add the car to carSales array
PAUSE;
break;
case 'B': // OUTPUT STATS
outputPrice(&carSales[500], &carsSold, &avgCarPrice, &totalRevenue);
PAUSE;
break;
case 'Q': // Quit Program
printf("Thanks....\n");
break;
default: // Invalid Selection
printf("Invalid selection...try again!\n");
PAUSE;
break;
} // end switch
} while (userSelection != 'Q');
PAUSE;
} // end of main
// DISPLAY THE MENU
void displayMenu() {
CLS;
printf("========== MAIN MENU ==========\n");
printf("A. ENTER NEW CAR\n");
printf("B. OUTPUT STATS\n");
printf("Q. QUIT\n");
printf("Enter your choice: ");
} // end displayMenu
// FLUSH
void flush() {
while (getchar() != '\n');
} // end flush
// GET THE USER CHOICE
char getUserChoice() {
char result;
displayMenu();
scanf("%c", &result); FLUSH;
result = toupper(result);
return result;
} // end getUserChoice
int newSale(int *price, char *firstTime, char *veteran, char *student, char *lastDay) {
// ASK QUESTIONS
printf("What is the sticker price of the car?\n");
scanf("%i", &price);
printf("Are you a first time buyer? (y/n)\n");
scanf("%s", firstTime);
printf("Are you a veteran? (y/n)\n");
scanf("%s", veteran);
printf("Are you a student (y/n)\n");
scanf("%s", student);
printf("Is it the last day of the month? (y/n)\n");
scanf("%s", lastDay);
// CALCULATE PRICES
if (*lastDay == 'y') { // car is discounted by 3% if user said yes
*price = *price - (((int)(*price) * 3) / 10); // (int) is added to keep the cocmpiler from complaining due to an implicit cast to floating point.
}
if (*firstTime == 'y') { // if it's the user's first time buying, $100 is given in credit
*price = *price - 100;
}
if (*student == 'y' && firstTime == 'y') { // car is discounted by $200 if user is a student and first time buyer- they also recieve the other discounts
*price = *price - ((int)(*price) - 200);
}
if (*veteran == 'y') { // veterans recieve 2% off the final price of the car
*price = *price - (((int)(*price) * 2) / 10);
}
return price;
}
int outputPrice(int carSales[ ], int *carsSold, int *avgCarPrice, int *totalRevenue) {
printf("The total cars sold is: %i", carsSold); // Display the amount of cars sold
for (int i = 0; i < 500; ++i) { // add all the prices in carSales
*totalRevenue = *totalRevenue + carSales[i];
}
printf("The Average car sold price: %i", avgCarPrice); // Display the avg price of the cars
printf("Total revenue: %i", totalRevenue); // Display total revenue
return;
}