How to countdown to 0 C++ - 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;
}

Related

I'm having issues with my for_each function and my finding prime function right now

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template <typename t>
void find_prime(const vector<t>& V)
{
int num;
bool is_prime = true;
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
is_prime = false;
break;
}
}
if (is_prime) {
cout << "This is the prime number: " << num << endl;
}
else {
cout << "This number is not a prime number: " << num << endl;
}
}
int main() {
int input;
vector<int> num;
cout << "please enter a number larger than 1: ";
cin >> input;
for (int i = 0; i < input; i++) {
cout << i << endl;
}
num.push_back(input);
for_each(num.begin(), num.end(), find_prime<int>);
}
Right now I'm writing currently doing a project that has a user populate the arry with a singular number, and then display all the numbers from 2 up to the number the user entered. After doing so it should then step through the vector and pass each element of the array through the find_prime function. Though I do not know why my program is deciding it wants to start crashing.
This is a simpler implementation of your code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
std::string find_prime(int num) {
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
return "This number is not a prime number: ";
}
}
return "This is a prime number: ";
}
int main() {
int input;
cout << "please enter a number larger than 1: ";
cin >> input;
for (int i = 2; i <= input; i++) {
cout << find_prime(i) << i << endl;
}
}
The above code takes in a number, and then for every number starting from 2 up to the number entered, the computer prints "This is a prime number: " <num> for primes and "This number is not a prime number: " <num> for non-primes.
Here, I have removed the usage of templates because that's completely unnecessary. For the same, I have also removed the vector you created. But in case you need all of that, you can use the below code, but I strongly advise the code provided earlier:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template <typename t>
std::string find_prime(t num) {
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
return "This number is not a prime number: ";
}
}
return "This is the prime number: ";
}
int main() {
int input;
vector<int> nums;
cout << "please enter a number larger than 1: ";
cin >> input;
for (int i = 2; i <= input; i++) {
nums.push_back(i);
}
for (auto& i : nums) {
cout << find_prime(i) << i << endl;
}
}

How do I output the max int from user input?

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

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;
}

Roman Numeral Output in C++ is always "-858993460", not sure why?

I am relatively new to C++ and have some experience in Java with classes and functions but now very much, so this program is giving me some issues. Below is the code I have, everything seems right now to me and even though I have set "num" to 0, it always prints out "-858993460".
Here are my header files:
#include <string>
using namespace std;
class romanType
{
public:
void setRoman(string n);
void romanToPositiveInteger();
void printPositiveInteger() const;
romanType();
romanType(string n);
void printNum();
private:
string romanString;
int num;
};
Here is my implementation file:
#include "stdafx.h"
#include <iostream>
#include <string>
#include "romanType.h"
using namespace std;
int value(char num) {
if (num == 'I')
return 1;
if (num == 'V')
return 5;
if (num == 'X')
return 10;
if (num == 'L')
return 50;
if (num == 'C')
return 100;
if (num == 'D')
return 500;
if (num == 'M')
return 1000;
return -1;
}
void romanType::setRoman(string n) {
romanString = n;
}
void romanType::romanToPositiveInteger() {
num = 0;
for (int i = 0; i < romanString.length(); i++)
{
// Getting value of symbol s[i]
int s1 = value(romanString[i]);
if (i + 1 < romanString.length())
{
// Getting value of symbol s[i+1]
int s2 = value(romanString[i + 1]);
// Comparing both values
if (s1 >= s2)
{
// Value of current symbol is greater
// or equal to the next symbol
num = num + s1;
}
else
{
num = num + s2 - s1;
i++; // Value of current symbol is
// less than the next symbol
}
}
else
{
num = num + s1;
i++;
}
}
}
void romanType::printPositiveInteger() const {
cout << num << endl;
}
romanType::romanType(string n) {
romanString = n;
}
romanType::romanType() {
}
void romanType::printNum() {
cout << num << endl;
}
And here is my main file:
#include "stdafx.h"
//Main program
#include <iostream>
#include <string>
#include "romanType.h"
using namespace std;
int main()
{
romanType roman;
string romanString;
while (romanString != "EXIT") {
cout << "Enter a roman number: ";
cin >> romanString;
roman.printNum();
roman.setRoman(romanString);
cout << "The equivalent of the Roman numeral "
<< romanString << " is ";
roman.printPositiveInteger();
cout << endl;
cout << endl;
}
//Pause the program
std::cout << "\n\n---------------------------------\n";
system("pause");
//Exit the program
return EXIT_SUCCESS;
}
As I said previously, I am currently held up on the output part, but since I am new and this code is most likely horrid, I am accepting any critique on it. I will be a pretty busy today with work and wont be able to implement any suggestions until the next day, but I will get back to anyone that has a solution as soon as I am able to! Thanks in advance for any help :)
You need to call roman.romanToPositiveInteger() at some point between roman.setRoman(romanString); and roman.printPositiveInteger();

How to fill the separators between integer without the redundant one?

I am trying to get the factors of positive integer. What I want is 8 = 2*2*2. However, what I get is *2*2*2. How can I get ride of the first *? Is there a standard way to better describe this situation?
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int num, i = 2;
const char separator = '*';
cout << "Input a positive integer: ";
cin >> num;
while(num !=1){
while((num % i) != 0){
i++;
}
cout << setw(2) << setfill(separator) << i;
num = num/i;
}
}
Input a positive integer: 8
*2*2*2
Use a separator that is updated. Start with "" and set to "*" thereafter.
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int num, i = 2;
const char *separator = "";
cout << "Input a positive integer: ";
cin >> num;
do {
while((num % i) != 0){
i++;
}
cout << separator << i;
separator = "*";
num = num/i;
} while (num > 1);
}
Also changed to do loop to cope with num == 1 and num == 0 which print nothing in OP's original code. Code could use unsigned as a further extension/ protection.
One way to accomplish that is by outputting setfill(separator) only when num is not equals to 1.
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int num, i = 2;
const char separator = '*';
cout << "Input a positive integer: ";
cin >> num;
while(num !=1){
while((num % i) != 0){
i++;
}
cout << setw(2) << i;
num = num/i;
if (num != 1)
cout << setfill(separator);
}
}