Corey Posted August 5, 2011 Share Posted August 5, 2011 Hi guys I am working on a c++ program atm and I ran into a bit of formatting issue. My program is supposed to look like this but instead this is what it looks like I'm sure it can't be that complicated to fix, but i am stumped :| #include <iostream> #include <string> #include <iomanip> using namespace std; int main() { double sales[3][4]= {{1000.00,2000.00,3000.00,1000.00},{2000.00,3000.00,1000.00,2000.00},{3000.00,1000.00,2000.00,3000.00}}; cout<<"\t\tQtr1\t\tQtr2\t\tQtr3\t\tQtr4"<<endl; for(int p=0;p<3;p++) cout<<"\nDivision "<<p+1; for(int i=0;i<3;i++) { for(int j=0;j<4;j++) cout<<fixed<<showpoint<<setprecision(2)<<sales[i][j]<<"\t\t"; cout<<endl; } cout<<endl; return 0; } Quote Link to comment Share on other sites More sharing options...
monkeysmack Posted August 5, 2011 Share Posted August 5, 2011 Your loops are not properly nested. Get rid of the first for loop and put cout<<"\nDivision "<<p+1; inside the brackets of the next for loop and above the last for statement. Change the p in cout<<"\nDivision "<<p+1; to i. #include <iostream> #include <string> #include <iomanip> using namespace std; int main() { double sales[3][4]= {{1000.00,2000.00,3000.00,1000.00},{2000.00,3000.00,1000.00,2000.00},{3000.00,1000.00,2000.00,3000.00}}; cout<<"\t\tQtr1\t\tQtr2\t\tQtr3\t\tQtr4"<<endl; for(int i=0;i<3;i++) { cout<<"\nDivision "<<i+1; for(int j=0;j<4;j++) { cout<<fixed<<showpoint<<setprecision(2)<<sales[i][j]<<"\t\t"; } cout<<endl; } cout<<endl; return 0; } Quote Link to comment Share on other sites More sharing options...
Corey Posted August 5, 2011 Author Share Posted August 5, 2011 thanks monkeysmack! I am still a noob at this stuff. knew my fellow would rescue me! Quote Link to comment Share on other sites More sharing options...
blazer Posted August 5, 2011 Share Posted August 5, 2011 Java > C++ Quote Link to comment Share on other sites More sharing options...
monkeysmack Posted August 5, 2011 Share Posted August 5, 2011 Us s have to stick together! Yes, most of my coding these days is in Java. Quote Link to comment Share on other sites More sharing options...
Corey Posted August 5, 2011 Author Share Posted August 5, 2011 I took a java class last semester. Absolute Disaster! I think it was my teacher mostly Quote Link to comment Share on other sites More sharing options...
blazer Posted August 5, 2011 Share Posted August 5, 2011 I took a java class last semester. Absolute Disaster! I think it was my teacher mostly actually if you really want to be a good programmer C++ is a good choice cause most Java IDES is about drag things and stuff :/ .. unlikely C++ that you have to do everything manually function by function , Old school . Quote Link to comment Share on other sites More sharing options...
rolf Posted August 5, 2011 Share Posted August 5, 2011 Java is, well, easy. It's programming, but for everything you want to do, there is already a function. For PHP for example, that is even more likely. C++ is only programming where nothing is free. Anyway, a really easy tip. Start indenting correctly (and use always {} for if/for/etc) and you could have seen immediately what your problem was. Your first for-loop is for each row, your second is for each column in a row. Ow, small note, which I usually do wrong every time. std::endl is really endline, "\n" is more the C-variant (and not really correct, for Windows you actually would have to use either \r\n or \n\r, I always forget). Quote Link to comment Share on other sites More sharing options...
Corey Posted August 5, 2011 Author Share Posted August 5, 2011 thanks for the tips Quote Link to comment Share on other sites More sharing options...
Kladkakan Posted August 6, 2011 Share Posted August 6, 2011 why am i reading this, i don't understand a thing in this topic Quote Link to comment Share on other sites More sharing options...
parrot Posted March 29, 2012 Share Posted March 29, 2012 std::endl and /n seem to be very similar, but I already ran into one program where the buffer flushing property of std::endl made the difference between running correctly and not running correctly, fwiw Quote Link to comment Share on other sites More sharing options...
rolf Posted March 29, 2012 Share Posted March 29, 2012 ^^ way to revive an old topic.... Anyway, the difference between \n and std::endl are a bit strange. Actually, you should compare \n and \r\n, \n is for Linux and \r\n is actually required for Windows. Although \n usually also works for Windows, with std::endl you solve that problem always at once for both environments. Quote Link to comment Share on other sites More sharing options...
parrot Posted March 29, 2012 Share Posted March 29, 2012 (edited) wow no kidding huh? Guess I should check the date next time. My professor taught us \n and std::endl both, but all we're doing atm is console programs in windows. Edited March 29, 2012 by parrot Quote Link to comment Share on other sites More sharing options...
uncasid Posted April 2, 2012 Share Posted April 2, 2012 Most of my coding as of late has been php / c#... lots of c#. I have to tell you, I got really sick of using MFC, my god... I don't think MS could make anything more confusing than that Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.