How do I output the max int from user input? - c++

I wrote a program that should output the maximum int from user input:
15 20 0 5 -1
should output Max: 20
There seems to be an infinite loop because it never gets to the final cout statement. The while loop should stop with a negative number. It's not outputting anything right now.
Here is my code:
#include <iostream>
using namespace std;
int main()
{
int currV;
int maxSoFar;
// 15 20 0 5 -1
// should output 20
cin >> currV;
if (currV > 0)
{
maxSoFar = currV;
}
while (currV > 0)
{
if (currV > maxSoFar)
{
maxSoFar = currV;
}
cin >> currV;
}
cout << "Max: " << maxSoFar;
}

Firstly, I recommend formatting your code better.
Furthermore, your code looks quite confusing and I recommend something like this for what you are trying to do:
#include <iostream>
using namespace std;
int main() {
int currV = 0;
int maxSoFar = 0;
while (cin >> currV)
{
if(currV < 0)
{
cout << "Max: " << maxSoFar;
return 0;
}
if(currV > maxSoFar)
{
maxSoFar = currV;
}
}
}

You need to initialize maxSoFar. Reading/using an uninitialized variable will cause undefined behaviour, then afterward anything could happened. This could be solve by initializing maxSoFar = -1; beforehand.
You can use max() to get maximum value of 2 integers as well.
#include <iostream>
using namespace std;
int main()
{
int currV, maxSoFar = -1; //initializing maxSoFar
while(cin >> currV)
{
if (currV < 0) {break;}
maxSoFar = max(maxSoFar, currV);
}
cout << "Max : " << maxSoFar; return 0;
}
Result:
5 20 0 5 -1
Max : 20
Also, see Why is "using namespace std;" considered bad practice?
Related : What happens to a declared, uninitialized variable in C? Does it have a value?

#include <iostream>
using namespace std;
int main()
{
int currV, maxSoFar = -1;
if (cin >> currV && currV >= 0)
{
maxSoFar = currV;
while (cin >> currV && currV >= 0)
{
if (currV > maxSoFar)
{
maxSoFar = currV;
}
}
}
cout << "Max: " << maxSoFar;
}
Online Demo

int currV;
int maxSoFar = 0; // if all integers are negetive than `maxSoFar` should be 0
while (cin >> currV)
{
if (currV < 0) break;
maxSoFar = max(maxSoFar, currV);
}
cout << "Max: " << maxSoFar;
see this - https://isocpp.org/wiki/faq/input-output#stream-input-failure

Related

Why is my code stopping prematurely? what have i done wrong?

I'm just starting so I'm trying to write a program which determine if a number is positive or negative.
#include <iostream>;
int step_function(int x) {
int result = 0;
if (x > 0)
result = 1;
else if (x < 0)
result = -1;
else
result = 0;
return result;
}
using namespace std;
int main() {
int num;
cout<< "please enter number : ";
cin >> num;
int a = step_function(num);
if (a == 1)
printf("%d is positive", num);
else if (a == -1)
printf("%d is negative", num);
else
printf(" it is zero");
return 0;
}
There is a few things you should do:
First things first you should get yourself a Good Book for C++.
Second thing is read why using namespace std; is a bad idea.
Lastly here is your code fixed. You needed to remove the semicolon as well as removing the printf(). I also removed the using namespace std; which made it more readable.
#include <iostream>
int step_function(int); //Function prototype
int main() {
int num;
std::cout << "please enter number : ";
std::cin >> num;
int a = step_function(num);
if (a == 1)
std::cout << num << " is postive";
else if (a == -1)
std::cout << num << " is negative";
else std::cout <<" it is zero";
return 0;
}
int step_function(int x)
{
int result = 0;
if (x > 0) result = 1;
else if (x < 0) result = -1;
else result = 0;
return result;
}
Don't use semicolon after #include <iostream>.
I think for C++ cout is more standard whereas printf is from C.
You can also include printing of the text in the step_function. Also, it's better to use braces {} after if and else statements for clarity especially if the code becomes complex.
#include <iostream>
using namespace std;
void step_function(int x) {
if (x > 0) {
cout << x << " is positive" << endl;
}
else if (x < 0) {
cout << x << " is negative" << endl;
}
else {
cout << "it is zero" << endl;
}
}
int main() {
int num;
cout<< "please enter number : ";
cin >> num;
step_function(num);
return 0;
}

How to countdown to 0 C++

I need to count down to 0. I am only printing 0 to the screen. How can I print all the count-down characters to the screen? Below is the code I am using right now.
#include <stdio.h>
#include <iostream>
using namespace std;
class Solution {
public:
int num;
int numberOfSteps (int num)
{
while (num != 0)
{
if (num % 2 == 0)
{
num = num / 2;
cout << num;
}
else
{
num = num - 1;
cout << num;
}
}
}
};
int main () {
int num;
Solution myObj;
cin >> num;
cout << myObj.num;
}
You're passing the num to std::cout. You are also not calling numberOfSteps(...) anywhere in your code.
Replacing the line with cout << myObj.numberOfSteps(num); fixes the problem, but a tidier solution would be as follows:
#include <stdio.h>
#include <iostream>
void countDown (int num) {
while (num != 0) {
if (num % 2 == 0) {
num = num / 2;
std::cout << num << std::endl;
} else {
num = num - 1;
std::cout << num << std::endl;
}
}
}
int main () {
int num;
std::cin >> num;
countDown(num);
}
Class is not necessary as there is no state and the function is void since it does not return anything.
I am revisiting this question and have created a simpler solution than my original post:
#include <iostream>
using namespace std;
int num;
int main()
{
cout << "Please enter the number you would like to count down to zero : ";
cin >> num;
while (num > 0)
{
cout << num << endl;
num--;
}
cout << "The number is now zero.";
return 0;
}

Getting the previous and current number of sequence

I have a problem on how to get the previous and current number of a sequence of integers . The problem is that everytime the previous number is same as the current (for example 15 12 16 10 9 8 0 and both are 8). Any ideas how to change it ? Thanks . Here is my code so far:
#include<iostream>
using namespace std;
int main()
{
int currentNumber;
int input;
int previousNumber = 0;
cin >> input;
while (input > 0 && input!=0)
{
currentNumber = input;
cin >> input;
previousNumber = currentNumber;
}
cout << previousNumber << endl;
cout << currentNumber << endl;
return 0;
}
If I have understood correctly then what you need is the following
#include <iostream>
int main()
{
int currentNumber = 0;
int previousNumber = 0;
for (int input; std::cin >> input && input > 0; )
{
if (currentNumber) previousNumber = currentNumber;
currentNumber = input;
}
std::cout << previousNumber << std::endl;
std::cout << currentNumber << std::endl;
return 0;
}

Controlling input in c++ and display

Question :
Your program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything. More precisely... rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits.
Example
Input:
1
2
88
42
99
Output:
1
2
88
So that is the question, however i am still a beginner and unable to have an input tab like that. In my program, how should i modify it such that it still accepts numbers after 42, however, it does not print them? currently I am only able to terminate the input at 42.
#include <iostream>
using namespace std;
int main()
{
int A[100], num, i=0,k,count;
for(count = 0; count != 1;){
cin >> k;
if (k!=42){
A[i] = k;
i++;
}
else
count =1;
}
cout << endl;
for (count = 0; count <i; count ++){
cout << A[count] << endl;
}
}
You don't have to use array at all. You can print the value just after reading it. Exit when you read 42. This may help you.
#include <iostream>
using namespace std;
int main() {
// your code goes here
int n ;
for(; ;) {
cin >> n ;
if(n == 42) {
return 0 ;
}
cout << n << endl ;
}
return 0;
}
Pretty sure the easiest way to do so is to simply ask the user how many numbers they need to enter.
#include <iostream>
using namespace std;
int main()
{
int A[100], k, count;
cout << "How many numbers do you want to enter ? ";
cin >> count; //this is to count how many numbers the user wants to enter
for(int i(0); i < count; ++i) //put all the numbers user enters in your array
{
cin >> k;
A[i] = k;
}
cout << endl;
for (int i(0); i < count; ++i)
{
if (A[i] == 42) //if the element at index i is == 42 then stop displaying the elements
break;
else
cout << A[i] << " "; //else display the element
}
cout << endl;
return 0;
}
Else you would need to put everything in a string and parse it and i'm not quite sure how that goes as I am a beginner as well.
EDIT:
Actually here you go, I think that is correct and does exactly what you want.
Do keep in mind that if user enters p.e "1 88 442" it will output "1 88 4" because it found "42" in "442". But it should be okay because you precised input numbers should only be two digits max.
#include <iostream>
using namespace std;
int main()
{
string k;
getline(cin, k);
cout << endl;
for (unsigned int i(0); i < k.length(); ++i)
{
if (!((k[i] == '4') && (k[i+1] == '2'))) //if NOT 4 followed by 2 then display
cout << k[i];
else
break; //else gtfo
}
cout << endl;
return 0;
}
Use a bool value to control the execution of your code.
#include <iostream>
#define N_INPUT 100
#define THE_ANSWER 42
using namespace std;
int main()
{
int array[N_INPUT], i, input, count=0;
bool universeAnswered = false;
for (i = 0; i < N_INPUT; i++) {
cin >> input;
if (!universeAnswered)
{
if (input == THE_ANSWER) {
universeAnswered = true;
} else {
array[count] = input;
count++;
}
}
}
for (i = 0; i < count; i++) {
cout << array[i] << endl;
}
}
(My code was not tested)
You just have to have some state to see if you have seen 42 already, and only output if you haven't
#include <iostream>
int main()
{
bool output = true;
for (int n; std::cin >> n;)
{
output &= (n != 42);
if (output)
{
std::cout << n << std::endl;
}
}
return 0;
}

Where is the Error in my C++ code?

Here is the error screenshot: http://prntscr.com/9n6ybt
Here is the code:
#include <iostream>
using namespace std;
int main()
{
int a, b;
cin>>a>>b;
for(int i=a;i<=b;i++)
{
if (b%i==0)
{
cout << i << " ";
}
}
return 0;
}
for(int i=a;i<=b;i++)
{
if (b%i==0)
{
cout << i << " ";
}
}
Will give a division by zero if i == 0.
You'll have to check the input, or the value of i, for example:
for(int i=a; i<=b; i++)
{
if (i > 0 && b%i==0)
{
cout << i << " ";
}
}
If i == 0, b%i==0 will not be evaluated.
You are not handling the case where i might be 0 (division by 0) so b % i is indetermined. You can solve it by going this way:
if (i==0) continue;
You should handle the case division by "zero". When the value of i = 0 then the code fails and produce an exception.
You should do like this:
#include <iostream>
using namespace std;
int main()
{ int a, b;
cin>>a>>b;
for(int i=a;i<=b;i++)
{ if (i == 0)
continue;
else if (b%i == 0)
cout << i << " ";
}
return 0;
}