I'm stuck with an odd error message

Oct 27, 2023 at 1:14pm
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.

Thanks
Oct 27, 2023 at 1:46pm
Make sure you don't have any old instances of the program running when the program is recompiled.

Your program contains an infinite loop.
Oct 27, 2023 at 1:47pm
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.
Oct 27, 2023 at 3:00pm
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.
Oct 27, 2023 at 3:03pm
@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>
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++;
}


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>
using namespace std;

int main() {
	int total {};
	int i { 1 };

	while (i <= 10) {
		total += 3;
		++i;
	}

	cout << "Total: " << total << '\n';
}

Oct 27, 2023 at 3:18pm
Your missing 7 I need to start with 7 and increase by 3, 19 times.
Oct 27, 2023 at 3:22pm
OK the last post works Thank you
Oct 27, 2023 at 3:45pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main() {
	constexpr int start { 7 };
	constexpr int count { 19 };
	constexpr int toadd { 3 };

	int total {start};
	int i { 1 };

	while (i <= count) {
		total += toadd;
		++i;
	}

	cout << "Total: " << total << '\n';
}

Last edited on Oct 28, 2023 at 9:43am
Oct 27, 2023 at 8:36pm
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( )
{
   const int seven = 7;
   int       total = seven;
   int       i     = 1;

   while ( i <= 10 )
   {
      total += 3;
      std::cout << "Total: " << total << '\n';
      i++;
   }
}
Total: 10
Total: 13
Total: 16
Total: 19
Total: 22
Total: 25
Total: 28
Total: 31
Total: 34
Total: 37


http://coliru.stacked-crooked.com/a/a6c66069cc63c36e

With your while loop going from 1 to 10 by ones the loop will execute only 10 times, not 19.
Topic archived. No new replies allowed.