| | |
C++ For Loop Question
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Mar 2006
Posts: 6
Reputation:
Solved Threads: 0
Hello,
I have this for loop here, and everything is great except I can't figure out how to display 3 numbers per line instead of all number on one line. Can anyone help? Thanx!
#include <iostream>
using namespace std;
I have this for loop here, and everything is great except I can't figure out how to display 3 numbers per line instead of all number on one line. Can anyone help? Thanx!
#include <iostream>
using namespace std;
int main( )
{
int x;
cout << "Numbers between 5 and 12 (3 numbers per line)are:\n";
for (x = 5; x <= 12; x++)
{
cout << x << " ";
}
cout << endl << endl;
return 0; You can play games with modular arithmetic to avoid adding another variable, but sometimes it's just easier to count how many numbers you've printed. When you get to 3, print a newline and reset the counter:
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { int x, n = 0; cout<<"Numbers between 5 and 12 (3 numbers per line)are:\n"; for ( x = 5; x <= 12; x++ ) { cout<< x <<" "; if ( ++n == 3 ) { cout<<'\n'; n = 0; } } cout<<"\n\n"; }
New members chased away this month: 3
![]() |
Similar Threads
- C++ For Loop User Response (C++)
- Simple script loop question (Shell Scripting)
- C++ Loop Question Here (C++)
- while loop question (Python)
- how to use loop in this question (C++)
- need help w/for loop (C++)
- I need to re-write this code using a 'while' loop. (Java)
Other Threads in the C++ Forum
- Previous Thread: Overloading assignment operator
- Next Thread: BDE Alias for ACCESS 2003 User/pwd ??
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






