Jump to content

My C++ questions thread :P


12p

Recommended Posts

(Hope this is the right place to put this).

So, I think I'll try to make *something* on C++. Hope you can (and want to) help me :)

I've got this simple code:

#include <iostream.h> 
  
int n 
  
void main ( ) 
{ 
    char mytable[4] = { 't', 'e', 's', 't' }; 
    for ( n = 0; n < 4; n++ ) { 
         cout<<mytable[n]; 
    } 
    cout<<mytable; 
} 

And it says error:

5:declaration syntax error

I can't get why it says so. Hope you show me the way.

Note. Maybe I'll need further help (lol). That's why the name of the topic is like that

Edited by Guest
Link to comment

You're missing a semicolon after line 3. Also, you should refrain from declaring global variables like you did with int n on line 3. Read here to see why.

#include <iostream> 
  
void main ( ) 
{ 
    char mytable[4] = { 't', 'e', 's', 't' }; 
    for ( int n = 0; n < 4; n++ ) { 
        std::cout << mytable[n]; 
    } 
    std::cout << mytable; 
} 

This isn't exactly the correct subforum - this subforum is supposed to be for MTA-related development discussion. Moved to "General".

Link to comment

Disregard my comment about accessing the array, I had a brain fart regarding the for loop. Your original for statement was correct.

What's the point of the final std::cout << mytable; line? You can't output an array with one line like that - you need to use the for loop you have in order to output the array's contents properly. Don't know why I didn't notice that earlier either... must be this daylight saving stuff here in the US tripping me up. :)

Link to comment
  • MTA Team

If you want to be able to use short cout, cin and other std:: functions syntax (like in your first post), paste this line after includes section:

using namespace std; 

Otherwise you will have to add std:: prefix for each use of cout, cin etc. functions.

Link to comment
  • 2 months later...
  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...