Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Basic c++ project error

Options
  • 18-10-2008 7:50pm
    #1
    Closed Accounts Posts: 66 ✭✭


    Just started getting down on learning c++. i understand this probably seems very trivial but bare with me.
    #include <iostream.h>
    #include <Windows.h>
    using namespace std;
    int main(int argc, int *argv[])
    {   int a, b ;
       a= argv[1];
       b= argv[2];
    Beep( a, b )  ; 
    return 0 ;
    }
    
    and error from Dev-C++ bloodshed
    6 C:\Users\Admin\Documents\programs\Untitled3.cpp invalid conversion from `int*' to `int'
    i assume im converting from a pointer to a non pointer or vice versa but honestly im a little confused ive tried different variations of pointer vs non pointer but im a little suck here.


Comments

  • Registered Users Posts: 2,534 ✭✭✭FruitLover


    argv is an array of pointers to type int. So argv[1] is a pointer to type int, not an int.

    In other news, argv should be an array of pointers to type char. Your program is passed a bunch of strings, not a bunch of ints. It's then your responsibility to handle any necessary conversion.


  • Registered Users Posts: 2,699 ✭✭✭samhail


    check out http://www.cprogramming.com/tutorial/lesson14.html

    doesnt c++ use #include <iostream> instead of #include <iostream.h> too ?


  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    A couple of things:

    * You're including windows.h that contains all the stuff for Windows GUI programming, one of the things it requires is to use WinMain instead of main.
    * The parameters to the main function have to be declared as (int argc, char* argv[]), you're using the wrong type. So even if you solve this you will not get you're program to run.

    I assume Beep() takes two integer values, so you'll need to take in the args as chars and then convert them into integers, which can then be passed to the Beep() function.


  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    notsamhail wrote: »
    doesnt c++ use #include <iostream> instead of #include <iostream.h> too ?
    Modern C++ does yes, the only difference is that iostream.h does not specify any namespaces.

    Could be wrong here, but think its a legacy header for when the early C++ compilers that didn't support namespaces.


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    First of all argv[] is an array of strings. IE:
    int main(int argc, char * argv[])
    {
    

    Next since these are characters you will have to convert them to integers. I would do this with atoi
    a = atoi(argv[1]);
    b = atoi(argv[2]);
    


  • Advertisement
  • Closed Accounts Posts: 66 ✭✭Sparkicks


    Sweet guys thanks that points out a lot.
    i think there were multiple things wrong with that which had my experimenting with pulling things around to try fix it which ended it in a mess.
    i've got it to work now but out of interest why do i need to use char? i've used int with a cout and it seemed to display the output fine.
    (edit: no actually i just tested it no it didnt i get it now)
    the conversion worked really well aswell but i thought i could use-this- to convert them.

    iostream.h indeed should have been iostream i guess i was either looking at a c tutorial instead of c++ but my compiler only gave me a warning when i compiled with it.
    appriciate the fast and accurate responces guys


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Command line parameters passed in from the console are always strings (character array), in any language for that matter I'm guessing. You must convert to appropiate data type such as int then. Some compilers will allow you to compile but others might be stricter. Better to get it right.

    The reason for the char * argv[]:

    We passing in an array of strings but a string is an array of characters.
    An array name/reference is essentially a pointer to first element in array. Pointers = *.
    So essentially we have an array of pointers. Each pointer points to first character in each parameter.


Advertisement