Hey all, i'm new to the whole programming scene. I came to this site to learn the basics of C++ so I can have a better understanding of programming language so that I may later apply these skills to UT2004 and soon UT3.
Although I had scucessfully made some mutators in the past, making them took a very long time and by the time I was done I look back at the poorly put together code and still wonder what some of it means.
Anyhow, I am having a problem with this counting program that I threw together... This is driving me crazy. The only way i can get this piece of code to work is to put it inside the FOR loop. otherwise if I place it as a seperate IF statement, the program will completley pass it up. I will post the entire code for the program.
This works but not the way I intended. I wish to append "... finished!" to the end of the counting sequence instead of another comma.Code:#include <iostream> using namespace std; int main () { int m; int specified; int n; cout<<"Pick a number 1-50 : "; cin>> specified; cin.ignore(); cout<<"Pick method of sorting : \n ==================== \n 1 : multiple rows\n 2 : Single row \n ==================== \n \n"; cin>> m; cin.ignore(); if ( m == 1 ) { for (int n = 0; n < specified + 1; n++) { cout<< n <<endl; } } else if ( m == 2 ) { for (int n = 0; n < specified + 1; n++) { cout<< n <<", "; } } else { cout<<"bad input"; } if ( n == specified ) { cout<< n <<"... finished!"; } return 0; }
This works, but I was told by a friend that it is a redundant check and could be done better.
Some answers why this does not work outside the FOR loop will clear up alot of confusion... This same problem plagued me in U script...Code:else if ( m == 2 ) { for (int n = 0; n < specified + 1; n++) { cout<< n <<", "; if ( n == specified ) { cout<< n <<"... finished!"; } } }![]()



LinkBack URL
About LinkBacks




Anyhow, I have been experimenting with the beginners tutorial knowledge trying to make the program a bit more complex and support a few extra options. With alot of trial and error, this is what came out of the "counting program."