Like Tree2Likes
  • 1 Post By stahta01
  • 1 Post By stahta01

Errors inside a function C++ 2010 express

This is a discussion on Errors inside a function C++ 2010 express within the C++ Programming forums, part of the General Programming Boards category; Hello all.. I am currently making a mini-game, where the program will make a number, and the user will be ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    4

    Errors inside a function C++ 2010 express

    Hello all..

    I am currently making a mini-game, where the program will make a number, and the user will be able to enter the minimum and maximum of that number, and an amount of guesses..

    I am having trouble with it though, and i was hoping you could help me.

    Btw, don't mind the Printf's, it's all in danish ;-)

    Edit: There is no problems when compiling, and in the beginning where i have to press a key to start is okay as well, but when i am prompted to enter the first number, i enter that, press enter and the error comes up. the cmd window just freezes, and i can't close it.. But i can stop debugging inside visual c++, and it will close..
    My code is:
    Code:
    // Gæt et tal.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <conio.h>
    #include <cstdlib>
    #include <time.h>
    
    
    
    int input (int min, int max, int maxgaet)
    
    {
        
        printf("\nIndtast det mindste tal: ");
        scanf_s("%d", min);
    
        printf("\n Indtast det hoejeste tal: ");
        scanf_s("%d", max);
    
        printf("\n Indtast max antal gaet: ");
        scanf_s("%d", maxgaet);
    
        return min, max, maxgaet;
    
    }
    
    int random (int min, int max)
    
    {
    
        int random_integer;
        int range=(max-min)+1;
    
    
        srand((unsigned)time(0));
    
        
    
        for(int index=0; index<1; index++)
    
        random_integer = min+int(range*rand()/(RAND_MAX + 1.0));
    
        return random_integer;
    }
    
    int gaet (int random, char lav, char hoej, char rigtig)
    
    {
    
        int gaet;
        gaet = '0';
            printf("\nIndtast det tal du gaetter på:  ");
            scanf_s("%d", gaet);
    
        if (gaet < random)
        {
            lav = '1';
            rigtig = '0';
            hoej = '0';
        }
    
        else if (gaet == random)
        {
            rigtig = '1';
            hoej = '0';
            lav = '0';
        }
    
        else if (gaet > random)
        {
            hoej = '1';
            lav = '0';
            rigtig = '0';
        }
    
        return random, lav, hoej, rigtig;
    
    }
    
    void resultat (int antal, int maxgaet)
    
    {
        if (antal*2 <= maxgaet)
            printf("\nHip hip hurra! På kun %d forsoeg!", antal);
        else if (antal <= maxgaet)
            printf("\nTillykke, du gaettede det. Dog på %d forsoeg.\nPrøv igen, og se om du kan komme under halvdelen af tilladte gaet.");
        else
            printf("\nDesværre, du gættede det ikke indenfor dine tilladte gæt");
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        char start, lav, hoej, rigtig;
        int min, max, maxgaet, antal,rando;
        min = '0';
        max ='0';
        maxgaet ='0';
    
        printf("\nTryk en tast for at starte");
            start = _getch();
    
        input(min, max, maxgaet);
    
        random(min, max);
    
        antal = lav = hoej = rigtig = rando = '0';
        do
        {
        
        gaet(rando, lav, hoej, rigtig);
        antal++;
        }
        while ((antal < maxgaet) || (rigtig == 0));
    
        resultat(antal, maxgaet);
        
        return 0;
    }
    and the error i get is this:

    Name:  Fejl.jpg
Views: 950
Size:  81.8 KB

    I hope someone can help me figure out what is wrong, since it makes no sense to me :-)
    Last edited by Nightstaber; 12-08-2011 at 04:24 AM.

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    4
    After having sniffed around a bit, i've found some errors which are:

    'Gæt et tal.exe': Loaded 'D:\Skole\HOT\C++\Projekter\Gæt et tal\Gæt et tal\Debug\Gæt et tal.exe', Symbols loaded.
    'Gæt et tal.exe': Loaded 'C:\Windows\System32\ntdll.dll', Symbols loaded (source information stripped).
    'Gæt et tal.exe': Loaded 'C:\Windows\System32\kernel32.dll', Symbols loaded (source information stripped).
    'Gæt et tal.exe': Loaded 'C:\Windows\System32\KernelBase.dll', Symbols loaded (source information stripped).
    'Gæt et tal.exe': Loaded 'C:\Windows\System32\msvcr100d.dll', Symbols loaded (source information stripped).
    First-chance exception at 0x5277ff81 (msvcr100d.dll) in Gæt et tal.exe: 0xC0000005: Access violation writing location 0x00000030.
    Unhandled exception at 0x5277ff81 (msvcr100d.dll) in Gæt et tal.exe: 0xC0000005: Access violation writing location 0x00000030.

  3. #3
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    Might not have anything to do with your problem, but still avoid whitespace in your file / project name(s).

    EDIT: And another two: in your scanf statements, you forgot the address operator. Plus, you can't return more than one value from a function.

    Code:
    scanf("%d", &variable);
    Last edited by dennis.cpp; 12-08-2011 at 05:41 AM.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Debian Testing/Jessie / Linux 3.16-2-amd64 / KDE SC 4.14.1
    Compiler: gcc 4.9.1-16 with KDevelop 4.7.0

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    The error says your dereferencing a null pointer
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    4
    Hmm.. What can i do then, to make it work? Will fix the scanf sentences.. But about the returning from functions?

    And thanks

  6. #6
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    That's what programming is all about - finding a way to solve a given problem.

    An array could be one possible solution. One function per task could be another (which you should do anyway). Global variables could be a third (which you should avoid).
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Debian Testing/Jessie / Linux 3.16-2-amd64 / KDE SC 4.14.1
    Compiler: gcc 4.9.1-16 with KDevelop 4.7.0

  7. #7
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    Looked a little closer now; you seem to have a general misunderstanding in how to use functions. You should revise that with your textbook.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Debian Testing/Jessie / Linux 3.16-2-amd64 / KDE SC 4.14.1
    Compiler: gcc 4.9.1-16 with KDevelop 4.7.0

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    4
    I am currently studying C++, and yesterday we were told a bit about functions.. Have used functions before, but never used them were i needed an output from them, so i am still learning ;-)

    I gotta admit that it is still pretty unclear to me how to do it in the correct way with inputs and outputs.. But.. Ye.. Well.. I think i have an idea now..

    Our teacher is doing exams today, so he isn't available ;-)

  9. #9
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    Generally, you don't need any parameters if you don't really pass any values into your function. Passing an empty variable as you intend to do in your code doesn't even work at all, despite it's senseless. So leave the brackets empty, in your definitions as well as in your calls.

    Secondly, if you return a value from a function, you need to store it somewhere in the environment from which you call it. Otherwise it's lost, since your local function variables cease existing after the function terminates.

    Code:
    int somefunction() { 
        int your_integer;
        // do stuff
        return your_integer;
    }
    Code:
    // some code
    int outside_variable;
    outside_variable = somefunction();
    // more code
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Debian Testing/Jessie / Linux 3.16-2-amd64 / KDE SC 4.14.1
    Compiler: gcc 4.9.1-16 with KDevelop 4.7.0

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Quote Originally Posted by Nightstaber View Post
    I am currently studying C++, and yesterday we were told a bit about functions.. Have used functions before, but never used them were i needed an output from them, so i am still learning ;-)

    I gotta admit that it is still pretty unclear to me how to do it in the correct way with inputs and outputs.. But.. Ye.. Well.. I think i have an idea now..

    Our teacher is doing exams today, so he isn't available ;-)
    C++? Is that what you think it is or is that what your resource or teacher says?
    This is C.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    2,747
    Quote Originally Posted by Nightstaber View Post
    Code:
    // Gæt et tal.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <conio.h>
    #include <cstdlib>
    #include <time.h>
    It is better to NOT mix C++ and C headers; but, when you need to do, it is safer to do, the C++ headers first.

    It is safer to do the local headers ("headername") after the system headers (<headername>)


    Code:
    /* C++ version of C standard header */
    #include <cstdlib>
    /* standard C header */
    #include <time.h>
    /* non standard C header */
    #include <conio.h>
    /* local header */
    #include "stdafx.h"
    Tim S.

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    2,747
    Warning:

    You do realize that you are NOT setting min to number zero in this line of code?
    Code:
    min = '0';
    This is how you set an integer to the number zero
    Code:
    min = 0;
    Tim S.
    dennis.cpp likes this.

  13. #13
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    Quote Originally Posted by stahta01 View Post
    (...) but, when you need to do, it is safer to do, the C++ headers first.

    It is safer to do the local headers ("headername") after the system headers (<headername>)
    Sorry for hijacking, but since I've never heard that before, I'd be interested in a short explanation about the reasons.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Debian Testing/Jessie / Linux 3.16-2-amd64 / KDE SC 4.14.1
    Compiler: gcc 4.9.1-16 with KDevelop 4.7.0

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    2,747
    Quote Originally Posted by dennis.cpp View Post
    Sorry for hijacking, but since I've never heard that before, I'd be interested in a short explanation about the reasons.

    See this thread
    How do you order your include statement

    The error/warning if two headers do NOT work well together happens in the second header. For this reason, I want the header that I wish to fix to be second. The local headers are normally the ones I wish to fix. Rarely is it a good idea to change a system header. Also, if my header conflicts with a system header the error/warning is more likely to be hard to understand if I put them in the other order.

    Tim S.
    dennis.cpp likes this.

  15. #15
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    Thanks, back to topic.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Debian Testing/Jessie / Linux 3.16-2-amd64 / KDE SC 4.14.1
    Compiler: gcc 4.9.1-16 with KDevelop 4.7.0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual C++ 2010 Express - difference from 2008?
    By ulillillia in forum Tech Board
    Replies: 26
    Last Post: 08-01-2011, 10:08 PM
  2. Visual C++ 2010 Express Portable Programs
    By Bronx68 in forum C++ Programming
    Replies: 18
    Last Post: 08-01-2011, 04:54 PM
  3. Visual C++ 2010 Express Portable Programs
    By Bronx68 in forum Windows Programming
    Replies: 1
    Last Post: 08-01-2011, 10:07 AM
  4. C project in Microsoft Visual C++ 2010 Express?
    By alexbcg in forum C Programming
    Replies: 2
    Last Post: 12-08-2010, 02:39 PM
  5. Visual C++ 2010 express problems
    By dnj23 in forum Windows Programming
    Replies: 6
    Last Post: 08-10-2010, 07:16 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21