Here is my code there is not much to it. I am supposed to write some code to go from 1 to 10 and each time I add 3 to the total. I start at 7.
#include <iostream>
using namespace std;
int main()
{
const int seven = 7;
int total;
int i = 1;
while (i <= 10)
total = seven + 3;
total = total + 3;
cout << "Total" << total << endl;
i++;
}
This is the error that I get.
Severity Code Description Project File Line Suppression State
Error LNK1168 cannot open C:\C++ Code\homework 6 test\x64\Debug\homework 6 test.exe for writing homework 6 test C:\C++ Code\homework 6 test\homework 6 test\LINK 1
I was getting a blank console screen.
The linker complains that it can't write the output executable. That's typically because there's an instance of the program that's still running. Make sure you've terminated it before trying to recompile.
OK, I closed Visual Studio and then reopened it an I got a console screen with nothing in it. This is not the first time this has happened. Last time I got a line of text but could not enter anything. This time I just get a blank screen.
@mathman54 - When posting code you should use code tags so that the code is shown formatted:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
usingnamespace std;
int main() {
constint seven = 7;
int total;
int i = 1;
while (i <= 10)
total = seven + 3;
total = total + 3;
cout << "Total" << total << endl;
i++;
}
When shown like this, you can see that the while loop only controls the following statement and hence i isn't incremented in the while loop body. Neither is total initialised so can have any value at the start of the program.
go from 1 to 10 and each time I add 3 to the total
Using a while loop is this what is required?
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
usingnamespace std;
int main() {
int total {};
int i { 1 };
while (i <= 10) {
total += 3;
++i;
}
cout << "Total: " << total << '\n';
}
The code as originally posted only while looped on total = seven + 3;.
Your increment variable was never actually incremented since it was outside the while block of code. You ran smack dab into the infinite loop wall. Any statement(s) outside the while block never execute either, so you get no output.
Initializing total inside the while block resets its value each time through the loop.
If your total is supposed to start at 7 you need to initialize it before the while block, maybe something like this?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
int main( )
{
constint seven = 7;
int total = seven;
int i = 1;
while ( i <= 10 )
{
total += 3;
std::cout << "Total: " << total << '\n';
i++;
}
}