Jump to content

Recommended Posts

Posted

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

 

imag0277q.jpg

 

but instead this is what it looks like

 

ssssssc.png

 

 

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;


}

Posted

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;


}




Posted
:D thanks monkeysmack! I am still a noob at this stuff. knew my fellow :omgcat: would rescue me!
Posted

I took a java class last semester. Absolute Disaster! I think it was my teacher mostly :P

Posted

I took a java class last semester. Absolute Disaster! I think it was my teacher mostly :P

 

 

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 :P.

 

 

Posted

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).

Posted

thanks for the tips :D

  • 7 months later...
Posted

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

Posted

^^ 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.

Posted (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 by parrot
Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.