Thread: Help needed

  1. #1
    Banned
    Join Date
    May 2004
    Posts
    55

    Help needed

    Hi, whats wrong with this program?
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <cstdio>
    using namespace std;
    int main()
    {
        char input[30];
        cin>>input;
        if(input==1)
        {
            ofstream a_file("test.dll");
            cin>>input;
            a_file<<input;
            a_file.close();
        }
        else if(input==2)
        {
            ifstream b_file("test.dll");
            b_file>>input;
            cout<<input;
            b_file.close();
        }
        getchar();
        getchar();
        return 0;
    }
    Thanks for answer

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    A couple things that I can see... what problems are you having?

    A quick rundown includes mixing old and new header includes, comparing a character array to an integer, and a lack of error handling. Which one you need help with depends on what your exact problem is.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    #include <iostream.h> //bad
    #include <fstream.h> //bad

    if(input==1) //bad
    if(strcmp(input, "1") == 0) //good

    or

    char input[30]; //bad
    int input; //good

    if(input == 1)

    also

    "test.dll" is not good, although not necessarily bad. Try test.dat or test.ini or test.nfo or something instead... dll means it's a dynamically linked library, or in other words it means it's supposed to contain functions and stuff, although you don't have to use it like that.
    Last edited by Hunter2; 08-04-2004 at 10:17 AM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Banned
    Join Date
    May 2004
    Posts
    55
    thnx a lot!!! (not)
    this is right huh,
    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdio>
    using namespace std;
    int main()
    {
        int input[30];
        cin>>input;
        if(strcmp(input, "1") == 0)
        {
            ofstream a_file("test.ini");
            cin>>input;
            a_file<<input;
            a_file.close();
        }
        else if(input==2)
        {
            ifstream b_file("test.ini");
            b_file>>input;
            cout<<input;
            b_file.close();
        }
        getchar();
        getchar();
        return 0;
    }
    If you really wanna know Dev-C++ shows 69 errors instead of my 2 errors by that code,
    explain it better, please?

    -Noxir

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>thnx a lot!!! (not)
    You're welcome!!! (not)

    First of all,
    >>else if(input==2)
    You didn't change that.

    Second, if you want help fixing errors, you need to post what the errors are. Preferably only the first 2 or 3, because the rest of them will fix themselves after the first few are fixed.

    >>explain it better, please?
    The .h means you're using old-style headers, as opposed to the new standard ones without the .h. Of course, if your compiler's an old compiler, then it might not support the new headers, so you'll have to stick the .h back on. And if you stick the .h back on, you need to get rid of "using namespace std;". And if your compiler doesn't support new headers, I'm not sure what it'll do with <cstdio>. Try <stdio.h> if <cstdio> is giving you errors. Next, the problem with if(input == 1) is that input is an array. This is C++, not VB. An array (or C-String if you prefer) when compared with an integer gets converted into a pointer, meaning 99.9999% of the time if(input == 1) will be false. Same with if(input == 2).

    And if you didn't know how it worked, you should have asked instead of copying and pasting the quick-fixes I posted.

    And just for future reference, the number of errors has nothing to do with how broken something is. You can have very broken code with no errors at all, as you will probably learn sometime in the future.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Banned
    Join Date
    May 2004
    Posts
    55
    cstdio isn't giving errors and yes I need .h after iostream and fstream, Thnq for answer (not!!)

  7. #7
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by Noxir
    ...yes I need .h after iostream and fstream...
    in C++ you should not use .h after those headers. you should use:
    #include <iostream>
    #include <fstream>

    if your input is going to be type char, use strncmp. If its int use ==. Mixing them isnt gonna work.

  8. #8
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Hey Noxir, a couple suggestions:
    • Thread titles should be descriptive (e.g. not "help needed"). I think it is safe to assume most threads are started because help is needed.
    • You have not yet told us what the problem is. When you incorrectly implemented Hunter2's suggestions, you got compile errors. Is that what you need help on - fixing compile errors? If so, what are the errors? It may seem obvious to you, but it is not necessarily obvious to everybody else.
    • Sarcastic remarks regarding other peoples attempts to help you don't come off very well when you are the one who made the mistake(s).
    • Stick with <cstdlib>, <iostream>, and <fstream> - I have a feeling that you don't have a valid reason for requiring the .h for those include.
    Anyway, good luck on solving your problems, whatever they are.

  9. #9
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    int main()
    {
      int input;
      char* fInput=new char[100];
      char* fileread=new char[100];
      cin>>input;
      if (input == 1) {
        ofstream a_file("test.ini");
        cin>>fInput;
        a_file<<fInput;
        a_file.close();
      }
      else if (input == 2) {
        ifstream b_file("test.ini");
        b_file>>fileread;
        cout<<fileread;
        b_file.close();
      }
      getchar();
      getchar();
      return 0;
    }
    Fixed it.

    The error was that you cant compare an 'int' value with a 'const char*' value(when using strcmp)
    ----Solution: Get rid of strcmp, get rid of cstdio compleatly, replace with 'input == 1'

    Hope that helps you Read the error logs, most the time they tell you exactly whats wrong, and if not, they atleast tell you what line(mine does anyway) so you can atleast find the problem.
    EDIT: I just read that you have Dev-C++, so yours does have line check. Its the number beside the file location
    Last edited by LloydUzari; 08-05-2004 at 04:43 PM.

  10. #10
    Banned
    Join Date
    May 2004
    Posts
    55
    Quote Originally Posted by jlou
    Hey Noxir, a couple suggestions:
    • Thread titles should be descriptive (e.g. not "help needed"). I think it is safe to assume most threads are started because help is needed.
    • You have not yet told us what the problem is. When you incorrectly implemented Hunter2's suggestions, you got compile errors. Is that what you need help on - fixing compile errors? If so, what are the errors? It may seem obvious to you, but it is not necessarily obvious to everybody else.
    • Sarcastic remarks regarding other peoples attempts to help you don't come off very well when you are the one who made the mistake(s).
    • Stick with <cstdlib>, <iostream>, and <fstream> - I have a feeling that you don't have a valid reason for requiring the .h for those include.
    Anyway, good luck on solving your problems, whatever they are.
    Is there any good reason why I doesn't need .h? :P

  11. #11
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by Noxir
    Is there any good reason why I doesn't need .h? :P
    No. The .h suffix is depreciated, but it is still very valid code. In small projects, the reason .h was removed and namespaces created will never be a problem.

    However you will be kicking yourself in the butt for not learning to work with non .h code once your projects grow to a considerable size. Can't wait to see you try to work around the "error: ambiguous reference" that you're bound to get in large projects.

  12. #12
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63
    Its not standard anymore, and Dev-C++ knows it Dev can be a bastard, and he'll throw an error-ful jab or two if you add '.h'.
    Just use it without .h, and put std:: before all functions from the header.

  13. #13
    Banned
    Join Date
    May 2004
    Posts
    55
    ok, thnx Lithorien!! (not)

  14. #14
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by LloydUzari
    Just use it without .h, and put std:: before all functions from the header.
    In small projects, is it really worth the hassle?

  15. #15
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63
    Nope, waste of typing 5 more letters before each function
    I know what you mean though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 09:32 AM
  2. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  3. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM