Why does this code snippet print 10 infinitely in c++11? [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
Why does the following C++ code snippet keep printing 10 indefinitely?
int num = 10;
while (num >= 1)
cout << num << " ";
num--;

Your snippet is the equivalent of this when using braces:
int num = 10;
while (num >= 1)
{
cout << num << " ";
}
num--;
Meaning only the printing statement is part of the loop. What you want is this:
int num = 10;
while (num >= 1)
{
cout << num << " ";
num--;
}

If you want to use a while loop in c ++ you will have to enclose the inner code between {}, like this:
while(/* Condition */){
// Do something
}

You might be familiar with python, though in c++ scoping ain't linked to indentation.
With correct indentation, it would look the following:
int num = 10;
while (num >= 1)
cout << num << " ";
num--;
I would really recommend a formatting tool like clang-format here, as it would immediately gave you this feedback.
Scopes in C++ are done using {}.
As such, the code should be:
int num = 10;
while (num >= 1)
{
cout << num << " ";
num--;
}
If you don't specify the {}, only the next statement is part of the scope.
In this case, I guess you are trying to learn while loops. Though this code is better fit for a for-loop.
int num = 10;
for (int num = 10; num >= 1; num--)
cout << num << " ";

Related

How to correctly implement a while loop that reads a sequence of inputs? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
Write a while loop that sums all integers read from input until a negative integer is read. The negative integer should not be included in the sum.
Ex: If input is 20, 45, 34, 5, -44, then the output is:
104
#include <iostream>
using namespace std;
int main() {
int numInput;
int numInts;
numInts = 0;
cin >> numInput;
while (numInput >= 0) {
cout << numInts << endl;
numInts = numInts + numInput;
}
cout << numInts << endl;
return 0;
}
The problem is that currently the condition inside the while loop doesn't depend on anything that you do inside the loop.
To solve this you can move the cin >> numInput to inside the condition as shown below:
int main() {
int numInput;
int sum = 0;
//take input and check it
while (cin >> numInput && numInput >= 0) {
sum += numInput;
std::cout <<"current sum is: "<<sum <<std::endl;
}
cout << sum << endl;
return 0;
}
Demo
The loop terminates when numInput is less than 0. In your code you read only one number. So if that number is greater than zero then you get an infinite loop because you never read a second number.
Your code should look something like this
cin >> numInput; // read the first number
while (numInput >= 0) {
cout << numInts << endl;
numInts = numInts + numInput;
cin >> numInput; // read the next number
}
See how the above code reads a new number each time around the while loop. This way when a negative number is entered the loop will terminate.

Why the output of my code has 4745728 as the output? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm making a simple program to count user balance whenever there's a positive integer input between 1-5 from user. I only make the code for the first option, but something weird is happening from the output, here's my code :
#include <iostream>
using namespace std;
int balance = 100000;
int iNet = 59900;
int internet(int iNet, int code, int remain){
if(code == 1){
remain = balance - iNet;
cout << "Price = " << iNet << endl;
cout << "Remaining credit = " << remain <<endl;
}
}
int main(){
int code, remain;
cin >> code;
cout << internet(iNet, code, remain);
return 0;
}
But when i run the program, it shows like this :
user input
1
program output
Price = 59900
Remaining credit = 40100
4745728
I have no idea where's the 4745728 coming from.
If you don't want to return anything from a function, make the return type void:
void internet(int iNet, int code, int remain){
// ^
if(code == 1){
remain = balance - iNet;
cout << "Price = " << iNet << endl;
cout << "Remaining credit = " << remain <<endl;
}
}
Then compiler will not allow you to print a rubbish value anymore and you will have to fix your main:
int main(){
int code, remain;
cin >> code;
internet(iNet, code, remain);
//^ don't print anything, just call the function
return 0;
}

Getting same output everytime [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
This a simple number guessing game. If you guessed the number right, it outputs "You win!!!", but if the number of tries (numberofguesses) is exceeded, it should output "You lose", but it is showing "You win!!!" even though I checked the values of numberofguesses, secretnum and guess after the while loop. Answer in simple words, I'm a beginner.
#include <iostream>
using namespace std;
int main()
{
int secretnum = 7;
int guess = 0;
int numberofguesses = 3;
while (secretnum != guess && numberofguesses != 0) {
cout << "enter your guess: ";
cin >> guess;
--numberofguesses;
}
if (secretnum = guess && numberofguesses != 0) {
cout << "You win!!!";
}
else
{
cout << "You lose";
}
}
You have mistaken the assignment operator = with the comparison operator ==.
In this line here:
if (secretnum = guess && numberofguesses != 0)
cout << "You win!!!";
Change it to:
if (secretnum == guess && numberofguesses != 0) {
cout << "You win!!!";

Quit while (true) loop in C++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
This is my simple while (true) loop:
while (true)
{
int i = 0;
++i;
cout << i << endl;
if (i == 5) {
break;
}
}
}
So break; isn't quitting the loop and the output is an infinite 1.
See this for Why use a for loop instead of a while loop?.
Now, coming to your question:
You are initializing i variable in each iteration of your while loop. Move the definition of i outside the while loop so that it's value can be updated.
Try this:
#include <iostream>
int main(void)
{
int i = 0;
while (true)
{
++i;
std::cout << i << std::endl;
if (i == 5)
break;
}
return 0;
}
Output:
1
2
3
4
5
Suggestion:
You can also use for loop as it is more appropriate to print a range of numbers.
#include <iostream>
int main(void)
{
for (int i = 1; i <= 5; i++)
std::cout << i << std::endl;
return 0;
}
The i is always initialized to 0 on each iteration. You should use this outside of the loop.
You are creating a new variable i in every run of the loop.
It is initalized to zero, then you increase it by one.
So after
int i = 0;
++i;
the variable i is always 1.
Solution:
int i = 0;
while (true) {
{
++i;
cout << i << endl;
if (i == 5) {
break;
}
}
}
or you can just use a simple for-loop:
for (int i = 0; i <= 5; i++)
{
cout << i << endl;
}

Having trouble looping through an array in c++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I seem to be looping through my array wrong, I've got it set up to prompt the user for a list of numbers and I am supposed to be comparing it to another number that the user sets.
#include <iostream>
using namespace std;
bool chk = true;
int main() {
/*
Write a program that asks the user to type 10 integers of an array and an integer s.
Then search the value s from the array and display the value of s if it is found in
the array otherwise print sorry not found..
*/
int userArray[10], i, greater = 0;
int s;
cout << "Enter a check number: \n";
cin >> s;
if (chk = true) {
//prompt for array list
for (i = 0; i < 9; i++) {
if (i == 0) {
cout << "Enter ten numbers: " << "\n";
cin >> userArray[i];
}
else {
cin >> userArray[i];
}
chk = false;
}
//loop through the array
for (int i = 0; i <= 10; i++) {
if (s = userArray[i]) {
//for testing
cout << userArray[i];
//cout << s;
}
else {
cout << "No match found!";
}
//I was just using this to pause the console and let me inspect result
cin >> greater;
return 0;
}
}
}
I assume the following code is where the problem lies. The idea is i set s = 2 enter in a list of numbers and then compare to s and print s if there is a match if not I print No match found. When I enter in a number that i know matches s it seems to print the first number in the array, but i thought since I loop through the numbers one by one in the for loop that it should display when it reaches the right number not when it stops. Thanks in advance
//loop through the array
for (int i = 0; i <= 10; i++) {
if (s = userArray[i]) {
//for testing
cout << userArray[i];
//cout << s;
}
else {
cout << "No match found!";
}
You are using a single equals sign. This is setting s to userArray[i] so it always evaluates to true. For comparisons, use double equal signs, like this:
if (s == userArray[i]) {...}
Also, your return statement is inside your loop (credit to #UnholySheep).
you are comparing with a single assignment operator = you should be using the equal operator instead ==
if (s = userArray[i]) with in the for loop is one example.
you also doing the same mistake in
if (chk = true)