Hey guy. I'm a new beginner in C++. I'm have some problem with my new code I have just written. I try to draw a triangles with from the characters "*". But it isn't work like I want. Here is it.

`#include <iostream>
using namespace
/* Draw a triangles */

int main() {


int a, b, c, d, e, f;
Cout<<"Enter a";
cin>>a;
cout<<"Enter b";
cin>>b;
c=(b/2)+1;
{for (d=0, d<b,d+1)
if (d=c)
cout<<"*";
else 
cout<<" ";}
endl;

{for (e=0, e<(a-1), e++)
{for (f=0,f<b,f++)
if (f=(c-e))
cout<<"*";
else cout <<" ";}
endl}
return 0;

`
The error in line 4 said "expected identifier before 'int'. And in line 6 and 7 it said ""Cout"(or "cin")wa not declaired in this scope. Please help me with it.

Recommended Answers

All 4 Replies

The two errors you mention come from the same source: line 2. The directive you want is use namespace std;; note the semi-colon, and the difference is the word use versus using, as well as the need to actually give the namespace you want to make visible (in this case, std, the standard library namespace).

You have a number of other problems with this code, most notably that you need to put the left braces after the conditional of the for() loops.

    for (d=0; d<b; d+1)
    {
        if (d=c)
            cout<<"*";
        else
            cout<<" ";
    }

Note, again, that the fields of the coditional need to be separated with semi-colons, not commas.

Also, keep in mind that C++ is case-sensitive; Cout and cout are two different identifiers.

Finally, I would recommend learning how to indent your code in a suitably helpful manner. Note how the nesting in the code is revealed when you indent the code effectively.

I think "Schol-R-LEA" meant to type:

using namespace std; // :)

It it always good, (especially for beginner coders), to start out with a working shell C++ program like 'Hello World' ... that compiles and gives the exact expected output ...

Then ... add a few lines ... (or a function, etc) ... and re-compile and test the output ... and fix till it works.

If there is a bug in the added new block of code, you will know 'where to look' ... just look in that added (small) block of code ... most probably :)

Always keep a copy of your last working 'step'. Then, if you can-not find the bug in your next stage of added code, you can always go back to a working program and add a smaller (compiling / working?) block of code.

// use comments to document what you want to do 
// at each stage
// this function prints out the values in
// an int array called 'ary' with size 'size'
void print( const int ary[], int size )
{
   for( int i = 0; i < size; ++i ) cout << ary[i] << ' ';
   // print newline at end
   cout << endl;
}

Yes, I got confused. I shouldn't post when only half awake...

That help was so thoughtful, when half sleeping, it must be awesome when awake :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.