c++ help

Discussion in 'Programming' started by izlik, Oct 1, 2008.

  1. #1
    I cant for my life figure out why this one is not working, cany anyone help me getting this code to run? :/ im getting about 10 errors so far :(

    #include "stdafx.h"
    #include <iostream> 
    #include <ctime> 
    
    
    using namespace std; 
    
    int main();
    int grid[3][3]; // grid
    int count = 1;
    
    for(int i = 0; i < 3; i++){
         for(int n = 0; n < 3; n++){
    
              if(i == 2 && n == 2) break; // dont give out a number to square 9
              grid[i][n] = count; // give out a number to square 1-8
              count++;
    
    		  {
    	return 0;
    		  }
    
         }
    }
    Code (markup):
     
    izlik, Oct 1, 2008 IP
  2. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It helps if you copy the errors here, too... give us as much information as you have! A lot of us don't have quick access to a compiler but can look at errors and tell you what they mean.
     
    TwistMyArm, Oct 1, 2008 IP
  3. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #3
    these are my errors that i got.

    \nummerspel.cpp(15) : error C2059: syntax error : 'for'
    1>c:\\nummerspel.cpp(15) : error C2143: syntax error : missing ')' before ';'
    1>c:\\nummerspel.cpp(15) : error C2143: syntax error : missing ';' before '<'
    1>c:\\nummerspel.cpp(15) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\\nummerspel.cpp(15) : error C2143: syntax error : missing ';' before '++'
    1>c:\\nummerspel.cpp(15) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\\nummerspel.cpp(15) : error C2086: 'int i' : redefinition
    1> c:\\nummerspel.cpp(15) : see declaration of 'i'
    1>c:\\nummerspel.cpp(15) : error C2059: syntax error : ')'
    1>c:\\nummerspel.cpp(15) : error C2143: syntax error : missing ';' before '{'
    1>c:\\nummerspel.cpp(15) : error C2447: '{' : missing function header (old-style formal list?)
    1>Build log was saved at "file://c:\nummerspel\Debug\BuildLog.htm"
    1>nummerspel - 10 error(s), 0 warning(s)
     
    izlik, Oct 1, 2008 IP
  4. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Oh I think I see the problem... you're defining the main function as essentially empty by doing:
    int main();

    instead, you mean:
    int main() {

    and then, naturally, another closing } at the end of the file.

    See if that helps.
     
    TwistMyArm, Oct 1, 2008 IP
  5. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #5
    al the other errors are gone, now im getting
    nummerspel.cpp(28) : fatal error C1075: end of file found before the left brace '{' at 'c:\\nummerspel.cpp(11)' was matched

    :/
     
    izlik, Oct 1, 2008 IP
  6. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Did you put in that last closing } at the end of the file?

    If you did, just work back through your file and make sure that you have matched all of your braces. Code that short shouldn't take very long at all to find the missing set.
     
    TwistMyArm, Oct 1, 2008 IP
  7. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #7
    now it's working! :) bellow is the correct code, however what's my problem now is that the code is supposed make a 3*3 grid and fill 8 of the panels with numbers and leave one empety. but when i run the program nothing appears in the CMD window at all :( doyou know why?

    #include "stdafx.h"
    #include <iostream> 
    #include <ctime> 
    
    
    using namespace std; 
    
    int main() {
    int grid[3][3]; // grid
    int count = 1;
    
    for(int i = 0; i < 3; i++){
         for(int n = 0; n < 3; n++){
    
              if(i == 2 && n == 2) break; // dont give out a number to square 9
              grid[i][n] = count; // give out a number to square 1-8
              count++;
    
    		  {
    	return 0;
    		  }
    
         }
    }
    }
    Code (markup):
     
    izlik, Oct 1, 2008 IP
  8. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Well the problem is that nowhere in that code do you even attempt to print anything... you're just populating an array.

    You need to printf (or cout) some numbers...
     
    TwistMyArm, Oct 1, 2008 IP
  9. MunnaMobile

    MunnaMobile Banned

    Messages:
    234
    Likes Received:
    58
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Go ahead now !!

    Error : Use of extra closing Braces.
    stdafx.h was of no use in this program so just commented !!

    
    //#include "stdafx.h"
    #include <iostream> 
    #include <ctime> 
    
    using namespace std; 
    
    int main() 
    {
    int grid[3][3]; // grid
    int count = 1;
    
    for(int i = 0; i < 3; i++){
         for(int n = 0; n < 3; n++){
    
              if(i == 2 && n == 2) break; // dont give out a number to square 9
    
              grid[i][n] = count; // give out a number to square 1-8
              count++;
    
    		  }
    	
      }
    return 0;
    }
    
    
    Code (markup):

    Return don't need opening and closing braces. and Return statement was needed out of Loop.
     
    MunnaMobile, Oct 1, 2008 IP
  10. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #10
    how could i do this for this grid? :( sorry if im a little noob. im still learning.
     
    izlik, Oct 2, 2008 IP
  11. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #11
    nwm about my other post, now it prints! :) however, the number become 1,2,3,4,5,6,7,8, how can i make it print one endl per row ?


    #include "stdafx.h"
    #include <iostream> 
    #include <ctime> 
    
    using namespace std; 
    
    int main() 
    {
    int grid[3][3]; // grid
    int count = 1;
    
    for(int i = 0; i < 3; i++) {
         for(int n = 0; n < 3; n++){
    
              if(i == 2 && n == 2) break; // dont give out a number to square 9
    
              grid[i][n] = count; // give out a number to square 1-8
              count++;
    		  cout << grid[i][n];
    		  }
      }
    return 0;
    }
    Code (markup):
     
    izlik, Oct 2, 2008 IP
  12. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #12
    After your:
    cout << grid[n];

    you should just be able to add something like:
    if (n == 2) {
    cout << endl;
    }
     
    TwistMyArm, Oct 3, 2008 IP