Home > other >  Program only prints out the first two lines of my code, and ignores the rest entirely
Program only prints out the first two lines of my code, and ignores the rest entirely

Time:01-11

So the problem I am facing here is that, the program runs only the first two lines of the code and entirely ignores the rest. I have tried rewriting it, I have also searched the internet for solution, but I found nothing and the problem continues to persist.

#include <iostream>
using namespace std;

struct customer{
    char A_ID[10];
    char pin[4];
    char amount[20];
    int input;
    int csnt;
}c;

int main()
{
 struct customer *p;
//  Welcome Note
 cout<<"Welcome to Absa,insert your card and enter you pin: "<<endl;
 cin.getline(c.pin,sizeof(c.pin));
 
//  Transaction Menu
 cout<<"1. Transfer"<<endl
 <<"2. Deposit"<<endl
 <<"3. Change pin"<<endl
 <<"4. Account Balance"<<endl;
 cin>>c.input;
 
//  Transaction Process
switch(c.input){
    case 1:
    cout<<"Enter amount: "<<endl;
    cin>>c.amount[9];
    cout<<"Enter recipient Account number: "<<endl;
    cin>>c.A_ID[10];
    cout<<"Payment of GHc "<<c.amount<<"made to "<<c.A_ID<<endl;
    cout<<"Enter 1 to confirm or 2 to cancel: "<<endl;
    cin>>c.csnt;
    if(c.csnt==1){
        cout<<"Transfer successful, thank you for Banking with Absa."<<endl;
    }
    else if(c.csnt==2){
        cout<<"Transfer cancelled, thank you for Banking with Absa."<<endl;
    }
}



return 0;

}

CodePudding user response:

Your p variable has not been initialized yet:

struct costumer *p;

That means it does not point to an instantiated object of type costumer. So this is how you can create an object on the heap:

std::unique_ptr<costumer> ptr { std::make_unique<costumer>{ } };

ptr will handle the deletion of the costomer object for you.

But there is really no need for you to use a pointer to costumer in this case. Just create it on the stack:

costumer cost; // here cost is stored on the stack so no need to deal with pointers
               // also struct keyword is not required here (in C  )

Then use the . operator to access its members like this:

std::cin >> cost.input;

Note: I don't understand what exactly you're trying to do here:

std::cin >>p->pin[4];

So my guess is that you are trying to assign a 4 digit number to pin array. This is not valid. But you may do this instead:

struct costumer {
...
    char pin[4]; // use a char array
...
}

// get exactly 4 `char`s from the user and store in `pin`
std::cin.getline( cost.pin, sizeof(cost.pin) );

// or like this
std::string temp_pin;
std::getline( std::cin, temp_pin );

if (temp_pin.size() == 4)
{ // for example print success message;
    std::strncpy( cost.pin, temp_pin.data(), sizeof(cost.pin) );
}
else
{ // repeat the input process }

Also, you seem to mix C and C for no particular reason. Try to use only one of them to prevent any problems that might arise due to the different behaviors of C and C functions. Use std::cout or std::format (with C 20).

  •  Tags:  
  • Related