#include <iostream>
#include "conio.h"
using namespace std;
void main(){
int a,b,c,result;
cout << "Enter Number to Count From: " << endl;
cin >> a;
if(a<=0){
cout << "Invalid";
}
else {
cout << "Enter Number to Count To: " << endl;
cin >> b;
_getch();
}
I am coming to C++ from having a Java experience. I know both are similar to one another, but I am having trouble with the if statement, if that's even what's going on here.
I am trying to make this what I made in Java, a counter that lets the user choose a number to count from, and a number to count to. I was just testing this code (not done) and it doesn't work, like if the user inputs 0 or less, it just moves on, ignoring the if statement.
Anyone know why? Can anyone help my code I am working on?
#include <iostream>
#include "conio.h"
usingnamespace std;
int main()//your main should return an int
{
int start=0;
int end=0;//initialize with 0
cout << "Enter Number to Count From: " << endl;
cin >> start;
if(start<0)//you used <=0 which means 0 or less than 0 thats why the problem occured on inputting 0
{
cout << "Invalid, Exiting now";
return -1;
}
cout << "Enter Number to Count To: " << endl;
cin >> end;
for(int i=start; i<=end;i++)//must use a for loop to print all number one by one from start till end
{
cout<<"No: "<<i<<endl;
}
int x;
cin>>x;// :D give it a pause, find a better way to do it as an exercise :)
return 0;
}