mercredi 12 septembre 2018

identifiers srand(), rand(), and time() not recognized

I'm just starting coding C++ in VSCode and am having trouble with using rand(time(0)); in any of my C++ programs. I have been working through a textbook for C++ and this is the first program to need #include <cstdlib>. I think this means it's an issue with my library, but I don't know how to go about fixing it. Any help on what I did wrong would be great.

Whenever i try to use srand();, rand(), or time() the intellisense says identifier 'rand' is undefined or similar for the other two function calls.
I'm including <cstdlib> and <ctime> and using namespace std; in my programs.
here's an example of my test case code saved as "rand test.cpp" in my workspace folder.

#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;

int main(){
    srand(time(0));
    int x = rand() % 10;
    cout<<x<<endl;
    return 0;
}

When I try to build the test program with the C/C++ Compile Run extension, the Compile & Run terminal spits out something similar to this:

PS C:\Users\Noah\Documents\Coding Resume> & g++ -st d=c++17 -Wall -Wextra 'c:\Users\Noah\Documents\Coding Resume\rand test.cpp' -o 'c:\Users\Noah\Documents\Coding Resume\rand test'
c:\Users\Noah\Documents\Coding Resume\rand test.cpp: In function 'int main()':
c:\Users\Noah\Documents\Coding Resume\rand test.cpp:7:11: error: too few arguments to function 'void sPS C:\Users\Noah\Documents\Coding Resume> & 'c:\Users\Noah\Documents\Coding Resume\rand test 
1`

my c_cpp_properties.json looks like this

{
"configurations": [
    {
        "name": "Win32",
        "includePath": [
            "${workspaceFolder}/**",
            "C:\\MinGW\\lib\\gcc\\mingw32\\6.3.0\\include\\c++",
            "C:/MinGW/lib/gcc/mingw32/6.3.0/include/c++/mingw32",
            "C:/MinGW/lib/gcc/mingw32/6.3.0/include/c++/tr1",
            "C:/MinGW/include",
            "C:/MinGW/lib/gcc/mingw32/6.3.0/include"
        ],
        "defines": [
            "_DEBUG",
            "UNICODE",
            "_UNICODE"
        ],
        "compilerPath": "C:\\MinGW\\bin\\gcc.exe",
        "cStandard": "c11",
        "cppStandard": "c++17",
        "intelliSenseMode": "clang-x64"
    }
],
"version": 4

}

for the original code I used when this problem popped up was:

    //checking randomness on a die
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int f, f1, f2, f3, f4, f5, f6;
    f = 0;
    f1 = 0;
    f2 = 0;
    f3 = 0;
    f4 = 0;
    f5 = 0;
    f6 = 0;

    for (int r = 1; r <= 6000; r++)
    {
        f = rand();
        f++;
        f = f % 6;
        switch (f)
        {
        case 1:
            f1++;
            break;
        case 2:
            f2++;
            break;
        case 3:
            f3++;
            break;
        case 4:
            f4++;
            break;
        case 5:
            f5++;
            break;
        case 6:
            f6++;
            break;
        default:
            cout << "something went wrong in the roller\n";
            break;
        }
        cout << "Face" << setw(13) << "Frequency" << endl
             << "1" << setw(13) << f1 << endl
             << "2" << setw(13) << f2 << endl
             << "3" << setw(13) << f3 << endl
             << "4" << setw(13) << f4 << endl
             << "5" << setw(13) << f5 << endl
             << "6" << setw(13) << f6 << endl
             << endl;

        return 0;
    }
}
//Expected output
/*
Face    Frequency
1       '~1000'
2       '~1000'
3       '~1000'
4       '~1000'
5       '~1000'
6       '~1000'
*/
//Actual output
/*
something went wrong in the roller
Face    Frequency
1            0
2            0
3            0
4            0
5            0
6            0
*/

Thanks for the help.




Aucun commentaire:

Enregistrer un commentaire