-4

call stack below: ucrtbased.dll!01905fcc() Unknown [Frames below may be incorrect and/or missing, no symbols loaded for ucrtbased.dll] [External Code] basketball.exe!_vfprintf_l(_iobuf * const _Stream, const char * const _Format, __crt_locale_pointers * const _Locale, char * _ArgList) Line 639 C++ basketball.exe!printf(const char * const _Format, ...) Line 954 C++

basketball.exe!main() Line 81 C++ [External Code]

//I keep getting an access violation.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int x;
    string playername, hometown,position,height,Class;
    int weight;

    cout << "Enter player number: ";
    cin >> x;

    switch(x){
    case 0: {
        playername = "Brandon Sampson";
        position = "Guard";
        weight = 193;
        hometown = "Baton Rouge, LA";
        height = "6-5";
        Class = "Sophomore";
        break;
    }
    case 1: {
        playername = "Dupo Reath";
        position = "Forward";
        weight = 235;
        hometown = "Perth, Australia";
        height = "6-10";
        Class = "Junior";
        break;
    }
    case 2: {
        playername = "Antonio Blakeney";
        position = "Guard";
        weight = 197;
        hometown = "Sarasota, Fla";
        height = "6-4";
        Class = "Sophomore";
        break;
    }
    case 3: {
        playername = "Elbert Robinton III";
        position = "Center";
        weight = 290;
        hometown = "Garland, Texas";
        height = "7-1";
        Class = "Junior";
        break;
    }
    case 4: {
        playername = "Skylar Mays";
        position = "Guard";
        weight = 205;
        hometown = "Baton Rouge, La";
        height = "6-4";
        Class = "Junior";
        break;
            }
    case 5: {
        playername = "Kieran Hayward";
        position = "Guard";
        weight = 195;
        hometown = "Sydney, Australia";
        height = "6-4";
        Class = "Freshman";
        break;
            }
    case 10: {
        playername = "Branden Jenkins";
        position = "Guard";
        weight = 180;
        hometown = "Maywood, Ill";
        height = "6-4";
        Class = "Junior";
        break;
    }

    }
            printf("Name:  %s", playername);
            printf("Position: %s\n", position);
            printf("Height: %s", height);
            printf(" Weight: %d", weight);
            printf("Hometown: %s\n", hometown);
            printf("Class: %s\n", Class);

            return 0;

}
  • 3
    Why .................... printf? – LogicStuff Nov 24 '16 at 19:49
  • Well, learned something new today. There is an undefined behavior when using printf with strings and you are probably inserting values for the missing case blocks (6, 7, 8, etc). Use std::cout instead. –  Nov 24 '16 at 19:56
  • replacing all of the print f with cout works, apparently. but why? can you not mix cin/cout with scanf/printf ? below works correctly. cout << "Player name: " << playername << endl; cout << "Position: " << position << endl; cout << "Height: " << height << " Weight: " << weight << endl; cout << "Hometown: " << hometown << endl; cout << "Class: " << Class << endl; – Rabbit84 Nov 24 '16 at 19:56
  • the cases that are being switched on are player numbers. This is an incomplete LSU basketball roster program (simple). Basically, you enter a player number and it prints out all of the information for the player who has that number. The whole program works by changing all of the printf instances with cout, basically. So, can you just not use printf when using case statements to decide string values? or just can't use printf when printing strings? – Rabbit84 Nov 24 '16 at 20:01
  • @Rabbit84 `printf` is a C function. C has no clue what a `std::string` is. This crashes (Undefined behaviour which manifests as a crash in this case) because `%s` tells `printf` that you will provide a pointer to a null-terminated character array. `std::string` is much more than just a char array. Basically, you stuck the square peg in the round hole and `printf` is too old and stupid to be able to tell the difference between the two. – user4581301 Nov 24 '16 at 20:02
  • is this why you have to put a .c_str() in order to get it to work? – Rabbit84 Nov 24 '16 at 20:22

1 Answers1

0

after reviewing the other post someone linked, printf apparently will work if you use printf("Follow this command: %s", myString.c_str()); . The .c_str() allows it to be used the way I was initially trying to do it. Thanks for the help and for the link. I missed that post earlier somehow. :)