Jump to content

C++


Corey

Recommended Posts

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;


}

Link to comment
Share on other sites

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;


}




Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 7 months later...

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.