Jetzt habe ich eine Aufgabe davon wieder lösen wollen und stecke aktuell fest.
Code:
#include <iostream>
int main()
{
int v1 = 0, v2 = 0;
std::cout << "Type a lower and a higher number in:" << std::endl;
//user input
std::cin >> v1 >> v2;
//keep executing the while as long as v1 is less than or equal to v2
while(v1 <= v2){
++v1; //add 1 to the momentous number
std::cout << v1 << std::endl; //gives out the actual number
}
std::cout << "These are all the number between" << v1 << " and " << v2 << "." << std::endl;
return 0;
}
Der User gibt eine niedrigere und eine höhere Zahl ein.
Zu v1 wird solange +1 addiert bis diese equivalent zu v2 ist. Nebenbei werden alle Zahlen im Zahlenraum zwischen v1 & v2 ausgegeben.
Als letztes soll der Text erscheinen zusammen mit den Zahlen vom user input.
Was nun aber passiert ist dass die Zahlen, beispielsweise 1 & 10 im letzten Text als 11 & 10 angezeigt werden.
Meine Frage: Warum?
Ich danke im voraus,
mfG
EDIT:
Code:
#include <iostream>
int main()
{
int v1 = 0, v2 = 0;
std::cout << "Type a lower and a higher number in:" << std::endl;
//user input
std::cin >> v1 >> v2;
//define v3 after v1 got user input's value
int v3 = v1;
//keep executing the while as long as v1 is less than or equal to v2
while(v1 <= v2){
++v1; //add 1 to the momentous number
std::cout << v1 << std::endl; //gives out the actual number
}
std::cout << "These are all the number between" << v3 << " and " << v2 << "." << std::endl;
return 0;
}
Legitim oder besser/einfacher lösbar?
MfG






