There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
I had an issue with the last instruction.
My first thought was to use totalSalesValue = (booksSold * bookCost) / 2; to output the cut, and it ran as intended, yet the instruction told me that I was doing it wrong. I shortly appeased it afterwards.
A bit odd to see only one answer as correct for something like this.
what was your solution? my following code is throwing an error
#include <stdio.h>
int main() {
// Variables set for you, do not change here
int booksSold = 200;
double bookCost = 9.99;
double totalSalesValue;
// Make your changes here
totalSalesValue = booksSold * bookCost / 2;
totalSalesValue = totalSalesValue * .5;
// Output logic, no need to change this
printf("You sold %d books and your take home was $%.2f\n", booksSold, totalSalesValue);
}
#include <stdio.h>
int main() {
// Variables set for you, do not change here
int booksSold = 100 + 200;
double bookCost = 9.99;
double totalSalesValue;
// Make your changes here
totalSalesValue = (booksSold * bookCost);
totalSalesValue = totalSalesValue / 2;
// Output logic, no need to change this
printf("You sold %d books and your take home was $%.2f\n", booksSold, totalSalesValue);
}
No idea how you get an error with your code, it works on my side.
But if it’s not letting you through to the next guide, I know why.
For one, it says you sold 200 MORE books, so it would be 100 + 200 instead of 200.
Plus, you simulated the publisher’s cut twice.
This is mine it always show error but the task show completed
#include <stdio.h>
int main() {
// Variables set for you, do not change here
int booksSold = 100;
double bookCost = 9.99;
double totalSalesValue = booksSold * bookCost;
// Make your changes here
int booksSold = + 200;
double totalSalesValue /= 2;
// Output logic, no need to change this
printf("You sold %d books and your take home was $%.2f\n", booksSold, totalSalesValue);
}
In the block under the comment // Variables set for you, do not change here , three variables have been declared. At the time of declaration, you must specify the type of variable (int or double or some other type).
You can change values of variables, but once a variable has been declared, you can’t declare it again. In the block under the comment // Make your changes here, you have mentioned their types again. When making assignments, we shouldn’t specify their types again.
// Declaration and Initialization
int x = 7;
// Assignment after declaration
int x = 5; // Incorrect - int keyword should be omitted
// as we have already specified type in declaration.
x = 5; // Correct
You have used incorrect syntax for addition assignment operator:
// You wrote:
int booksSold = + 200;
// It should be:
booksSold += 200;
After incrementing booksSold, you need to recalculate totalSalesValue. You calculated totalSalesValue when booksSold were 100. After additional sales, booksSold are 300, and totalSalesValue needs to be recalculated:
// You wrote:
int booksSold = + 200;
double totalSalesValue /= 2;
// It should be:
booksSold += 200;
totalSalesValue = booksSold * bookCost;
totalSalesValue /= 2;
If you fix all the issues, your final output should be:
"You sold 300 books and your take home was $1498.50"
If I wanted to store the value of updated booksSold variable in a different variable. Do I need to declare it first before carrying out the relevant process?
In the C programming language, you do need to declare variables before using them. However, the exact declaration depends on the context and the scope of the variables. Here’s an example:
#include <stdio.h>
int main() {
// Original value of booksSold
int booksSold = 100;
// Declare a new variable to store the updated value
int updatedBooksSold; // You can initialize it with some default value or leave it uninitialized
// Perform some operation to update booksSold
// For example, let's say you increase the sales by 10
booksSold += 10;
// Store the updated value in the new variable
updatedBooksSold = booksSold;
return 0;
}