8 Replies - 1052 Views - Last Post: 17 February 2012 - 10:51 AM Rate Topic: -----

#1 eiarzate  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 33
  • Joined: 21-January 10

josephus circle with 3 command line parameters.

Posted 16 February 2012 - 09:42 PM

Hi. This is a very simple c++ program that simulates a josephus circle with 3 parameters passed on the command line.
The program works on visual studio without the command line parameters. But the code has to be executed through the linux terminal using ./a.out m n p
The logic is fine it works except it will not compile in the terminal. If anyone out there is familiar with linux command line execution and can help me with the syntax it would be greatly appreciated.

Thanks.

#include <iostream>
#include <stdlib>
#include <cstring>
using namespace std;

struct node{
        int item;
        node* next;

        node(int x, node* t){
                item = x;
                next = t;
         }
};
typedef node *link;

int main(int argc, char *argv[]){
        int i;
        int N = atoi(argv[1]);
        int M;
        int count = 1;
        int m = atoi(argv[2]);
        int p = atoi(argv[3]);

        link t;
        t = new node(1, 0);
        t->next = t;
        link x;
        x= t;
        for(i = 2; i <= N; i++)
                x = (x->next = new node(i, t));
        while(x != x->next){

                if(count % 2 == 1){
                        M = m;
                }
                else{
                        M = p;
                }

                for(i = 1; i<M; i++){
                         x = x->next;
                }
                x->next = x->next->next;
                count++;
        }
        cout<< x->item << endl;
}





Is This A Good Question/Topic? 0
  • +

Replies To: josephus circle with 3 command line parameters.

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4223
  • View blog
  • Posts: 13,245
  • Joined: 25-December 09

Re: josephus circle with 3 command line parameters.

Posted 16 February 2012 - 09:49 PM

Quote

The logic is fine it works except it will not compile in the terminal.

What are your error messages when you try to compile your program? Show your complete compile line.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 eiarzate  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 33
  • Joined: 21-January 10

Re: josephus circle with 3 command line parameters.

Posted 16 February 2012 - 09:57 PM

In file included from /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include /c++/4.1.2/backward/iostream.h:31,
from josephus.cpp:1:
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/backward/ba ckward_warning.h:32:2: warning: #warning This file includes at least one depreca ted or antiquated header. Please consider using one of the 32 headers found in s ection 17.4.1.2 of the C++ standard. Examples include substituting the <X> heade r for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
josephus.cpp:3:21: error: cstring.h: No such file or directory
josephus.cpp:15: error: ‚typedef struct node* link‚ redeclared as different kind of symbol
/usr/include/unistd.h:757: error: previous declaration of ‚int link(const char*, const char*)‚
josephus.cpp: In function ‚int main(int, char**)‚:
josephus.cpp:25: error: expected `;' before ‚t‚
josephus.cpp:25: warning: statement is a reference, not call, to function ‚link‚
josephus.cpp:26: error: ‚t‚ was not declared in this scope
josephus.cpp:28: error: expected `;' before ‚x‚
josephus.cpp:28: warning: statement is a reference, not call, to function ‚link‚
josephus.cpp:29: error: ‚x‚ was not declared in this scope
[earzate285@redux1 pgm]$ nano josephus.cpp
[earzate285@redux1 pgm]$ g++ josephus.cpp
josephus.cpp:2:18: error: stdlib: No such file or directory
josephus.cpp:15: error: ‚typedef struct node* link‚ redeclared as different kind of symbol
/usr/include/unistd.h:757: error: previous declaration of ‚int link(const char*, const char*)‚
josephus.cpp: In function ‚int main(int, char**)‚:
josephus.cpp:25: error: expected `;' before ‚t‚
josephus.cpp:25: warning: statement is a reference, not call, to function ‚link‚
josephus.cpp:26: error: ‚t‚ was not declared in this scope
josephus.cpp:28: error: expected `;' before ‚x‚
josephus.cpp:28: warning: statement is a reference, not call, to function ‚link‚
josephus.cpp:29: error: ‚x‚ was not declared in this scope
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4223
  • View blog
  • Posts: 13,245
  • Joined: 25-December 09

Re: josephus circle with 3 command line parameters.

Posted 16 February 2012 - 10:18 PM

Your error messages do not match your source. The first error states that you are including iostream.h on line 1, however you are including iostream, which is the correct header. Your second line in the above code is incorrect: #include <stdlib> should be #include <cstdlib>, you are missing the 'c'.

Also you should never try to "hide" a pointer behind a typedef:
typedef node *link;

Plus you have an error on this line:

Quote

josephus.cpp:15: error: âtypedef struct node* linkâ redeclared as different kind of symbol
/usr/include/unistd.h:757: error: previous declaration of âint link(const char*, const char*)â

This is telling you that link is already being used by one of the headers included by your program. As I stated don't try to hide the pointer just declare the variable properly:
        //link t; // Don't hide that pointer.
        node* t;  // Just create the variable using the pointer.



Edit: Also you need to use meaningful variable names. It will make your debugging much easier.


Jim

This post has been edited by jimblumberg: 16 February 2012 - 10:27 PM

Was This Post Helpful? 1
  • +
  • -

#5 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: josephus circle with 3 command line parameters.

Posted 17 February 2012 - 05:53 AM

As for your original code, it compiles when I replace #include <stdlib> with #include <cstdlib>.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4223
  • View blog
  • Posts: 13,245
  • Joined: 25-December 09

Re: josephus circle with 3 command line parameters.

Posted 17 February 2012 - 07:28 AM

@Karel-Lodewijk
What operating system and compiler are you using? Did you change the typedef?

Jim
Was This Post Helpful? 0
  • +
  • -

#7 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: josephus circle with 3 command line parameters.

Posted 17 February 2012 - 08:32 AM

Quote

@Karel-Lodewijk
What operating system and compiler are you using? Did you change the typedef?


Ubuntu 11.10, but with g++ 4.7.0, doing some c++11 things so I went for a more recent gcc. I did not change the typedef. I don't see what it could be conflicting with either, never heard of int link(const char*, const char*).

Edit:
Well it seems to be a function from the posix ast.h header. It could be that this was pulled in indirectly with a previous stdlib version but not any more, these things happen.

This post has been edited by Karel-Lodewijk: 17 February 2012 - 08:46 AM

Was This Post Helpful? 1
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 4223
  • View blog
  • Posts: 13,245
  • Joined: 25-December 09

Re: josephus circle with 3 command line parameters.

Posted 17 February 2012 - 10:10 AM

Yea, my version of gcc is older and is including unistd.h which is where link is defined.


Jim
Was This Post Helpful? 0
  • +
  • -

#9 eiarzate  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 33
  • Joined: 21-January 10

Re: josephus circle with 3 command line parameters.

Posted 17 February 2012 - 10:51 AM

I added the c in front of <stdlib>
I also took out the typedef link *node
and the code works fine now.

Thanks all.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1