So I'm already stuck lol. Question is:
Write a program that prompts the user for 2 integers. Print each number in the range specified by these 2 integers.
I used the numbers 1 and 10 and I got the following output:
1, 2, 3, 4, 5, 6, 7, 8, 9,
The 1, and the , after 9 should not be here :S
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//This is what I wrote but it has some problem
#include <iostream>
usingnamespace std;
int main()
{
cout << "Enter 2 numbers to find all numbers in b/w them: " << endl;
int n1=0, n2=0;
cin >> n1 >> n2;
while(n2>n1)
{
cout << n1 << ", ";
++n1;
}
return 0;
}
#include<iostream>
using std::cout;
using std::cin;
using std::endl;
int main(){
int
number1,
number2;
cout<<"Enter 2 numbers to find all numbers in b/w them: "<<endl;
cin>>number1>>number2;
cout<<"The numbers between "<<number1<<" and "<<number2<<" are: "<<endl;
number1+=1;
number2-=1;
for(int counter=number1;counter<=number2;counter++){
cout<<counter<<' ';
}//end loop for
cout<<endl;
return 0; //indicates successful termination
}//end main
#include<iostream>
using std::cout;
using std::cin;
using std::endl;
int main(){
int
number1,
number2;
cout<<"Enter 2 numbers to find all numbers in b/w them: "<<endl;
cin>>number1>>number2;
cout<<"The numbers between "<<number1<<" and "<<number2<<" are: "<<endl;
number1+=1; //now you increment number1 by 1 e.g you entered 1 and 7
//so now number1 is equal to 2
while(number1<number2){ //loop from number1 -now is 2- through 1 before
//number2
cout<<number1<<" "; //print the numbers b/w number1 and number2
number1++;
}//end loop while
cout<<endl;
return 0; //indicates successful termination
}//end main
Enter 2 numbers to find all numbers in b/w them:
1 7
The numbers between 1 and 7 are:
2 3 4 5 6
counter is just an identifier for the variable, you can choose the name -identifier-
that you want for example i,j,k or counter my advice is that you pick up some good C++ book and learn the basics again, like VARIABLES give me your e-mail and i'll send you a link to download a good book.
There is no need for that in your code because they are overwritten a moment later. But it is a good idea to initialize your variables:
* If your code ever changes you won't bump into undefined behavour because you used it without initialization.
* Excessive assigment will be optimized away anyway.
Okay, I tried to separate the numbers with a "," but after the last number the "," remains here. I tried at random(without actually knowing it lol) to use an if statement but it doesn't seem to work lol.
Output: 2, 3, 4, 5, 6, 7, 8, 9,
See the "," after 9?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
usingnamespace std;
int main()
{
cout<<"Enter 2 numbers to find all numbers between them:"<<endl;
int number1, number2;
cin>>number1>>number2;
cout<<"The numbers between "<<number1<<" and "<<number2<<" are:"<<endl;
number1+=1;
while(number1<number2)
{
cout<<number1<<", ";
++number1;
}
return 0;
}
Check my code above or vlad from moscow one.
EDIT: vlad from moscow one does output upper bound :(
And it syntaxically wrong.
I realized, that you might doesn't know how to use ternary operator, so I replaced it with if:
Great explanation MiiNiPaa. Thanks :)
I tried it and it worked perfect. You just missed ++n1 after std::cout<<", "; but it's just fine :D Thanks buddy :)
More question:
Why std::cout and not using namespace std;
How does using namespace std; affect the whole program in case it's more complex? This is a simple program, so obviously it's not a big deal, but what happens in case of bigger, more complex programs, where different namespaces are used? Would it be as follows:
using namespace std;
using namespace x;
using namespace y;
using namespace z;
...etc
?