0

I'm creating a program wherein I need to login into a account and see the balance and due date, but some error occurred when I ran it. This is the error I encounter: [Error] cannot pass objects of non-trivially-copyable type 'std::string {aka class std::basic_string}' through '...'

I tried searching online on how to fix, but still not working

Here is my code:

#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
int main() {
    string names[2] = { "MARK", "EMILY" }, Ddate[2] = { "10/04/19","10/06/19" };
    int bal[2] = { 4500,6500 }, id_num[2] = { 1810784, 1810783 }, pword[2] = { 117611,594356 }, idnum, pw,i = 0;

    char check_bal,temp;
    printf("WELCOME TO STUDENT BILLING SYSTEM \n");
    printf("Would you like to check your balance? Y/N\n");
    scanf( "%c", &check_bal);
    temp = toupper(check_bal);
    check_bal = temp;

    if (check_bal == 'Y') {
        printf("ENTER YOUR ID NUMBER: ");
        scanf("%i", &idnum);
        printf("ENTER YOUR PASSWORD: ");
        scanf("%i", &pw);
        while (true) {
            if (idnum == id_num[i])
                break;
            else
                continue;
        }
        while (true) {
            if (pw == pword[i])
                break;
            else
                continue;
        }
        printf("HI %s, your balance is %i and the DUE DATE is: %s \n", names[i],bal[i],Ddate[i]);
}
else
    system("EXIT");

system("PAUSE");
return 0;

}

  • 1
    I suggest you invest in [a couple of good books about C++](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282), to learn how to use C++ input and output instead of the old C input/output. – Some programmer dude Sep 26 '19 at 18:28
  • 1
    All of those `printf` operations taking `%s` specifiers expect `const char*` as the argument. You're giving them `std::string` s. That won't work, and is done with multiple arguments. Do as @Someprogrammerdude suggests, and get a good book describing how to use C++ iostreams. – WhozCraig Sep 26 '19 at 18:31
  • Your compiler should tell you what line caused the error. – François Andrieux Sep 26 '19 at 18:31
  • @FrançoisAndrieux Considering that `system` creates a "shell" to run the commands, the command exits that shell only and not anything else, that function call is practically useless. – Some programmer dude Sep 26 '19 at 18:32
  • @Someprogrammerdude Thanks. – François Andrieux Sep 26 '19 at 18:35

1 Answers1

0

The code compiles just fine in g++, but outputs garbage. With g++ -Wall you get the sensible warning:

warning: format ‘%s’ expects argument of type ‘char*’, but argument 2 has type ‘std::__cxx11::string’ {aka ‘std::__cxx11::basic_string<char>’} [-Wformat=]

This is g++ version 8.3.0.

The code as is can be salvaged like this:

printf("HI %s, your balance is %i and the DUE DATE is: %s \n", names[i].c_str(),bal[i],Ddate[i].c_str());

But please, pay attention to the comments that recommend to use c++ style io.

g_bor
  • 1,077
  • 6
  • 14