Basically the please hit any key to enter doesn't show up in Visual C++.Thanks for help

main

//CS 310
/*Create a text file and input in certain data into the text
file*/
//Program one

#include <iostream>
#include <fstream>
#include "Account.h"
using namespace std;
int main()
{
    account b;
    char name1[256];
    cout<<"enter file name to be opened\n";
    cin>>name1;
    ofstream infile;
    infile.open(name1);
    if(!infile)
    {
        cout<<"File could not open\n";
        exit(1);
    }
    else
    {   
        while(b.get_account_number()!=-1)
        {
            if(b.get_account_number()==-1)
            {
                cout<<"account number is one\n";
                infile.close();
                //exit(1);
            }
            else
            {
            cout<<"enter while loop\n";
            cin>>b;
            infile<<b;
            }

        }
    }

    return 0;
}

Interface

//class used to create an account

#include <string>
using namespace std;
class account
{
private: 
    int account_Number;
    string name;
    string address;
    string city;
    string state;
    int zip_Code;
    double account_Balance;
public:

    //default account constructor
    account(int=0,  string="", string="", string="", string="", int=0, double=0);

    //sets the private variables above
    void set_account_number(size_t account_Number_Value);
    void set_title(string first_Last);
    void set_address(string location);
    void set_city(string county);
    void set_state(string loc);
    void set_zip_code(int zip);
    void set_account_balance(double user_balance);

    //gets the members value
    int get_account_number();
    string get_title();
    string get_address();
    string get_city();
    string get_state();
    int get_zip_code();
    double get_account_balance();

};

//overloaded operator to allow an object to be read in
istream& operator>>(istream&in,account& c);

//overload operator to an object to be read to the file
ostream& operator<<(ostream& out, account& z);

IMPLEMENTATION

#include "Account.h"
#include <iostream>
#include <iomanip>
using namespace std;

account::account(int accountNumberValue,string nameValue,string addressValue, string cityValue, string stateValue,
                 int zipCodeValue,double AccountBalanceValue)
{
    set_account_number(accountNumberValue);
    set_title(nameValue);
    set_address(addressValue);
    set_city(cityValue);
    set_state(stateValue);
    set_zip_code(zipCodeValue);
    set_account_balance(AccountBalanceValue);
}

void account::set_account_number(size_t account_Number_Value)
{
    account_Number=account_Number_Value;
}

void account::set_title(string first_Last)
{
    name=first_Last;
}

void account::set_address(string location)
{
    address=location;
}

void account::set_city(string county)
{
    city=county;
}

void account::set_state(string loc)
{
    state=loc;
}

void account::set_zip_code(int zip)
{
    zip_Code=zip;
}

void account::set_account_balance(double user_balance)
{
    account_Balance=user_balance;
}

int account::get_account_number()
{
    return account_Number;
}

string account::get_title()
{
    return name;
}

string account::get_address()
{
    return address;
}

string account::get_city()
{
    return city;
}

string account::get_state()
{
    return state;
}

int account::get_zip_code()
{
    return zip_Code;
}

double account::get_account_balance()
{
    return account_Balance;
}

istream& operator>>(istream&in,account& c)
{
    int number,zip_code,balance;
    string accountNumber;
    char name[256];
    char address[256];
    char city[256];
    char state[256];
    bool a =true;
    cout<<"enter account number";
    in>>accountNumber;
    while(a==true)
    {
    if(accountNumber.length()==10)
    {
        a=false;
        cout<<"enter your name\n";
        in.ignore();
        in.getline(name,256);
        cout<<"enter your address\n";
        in.getline(address,256);
        cout<<"enter your city\n";
        in.getline(city,256);
        cout<<"enter your state\n";
        in.getline(state,256);
        cout<<"enter your zip_code\n";
        in>>zip_code;
        cout<<"enter account balance\n";
        in>>balance;
        number=atoi(accountNumber.c_str());
        c=account(number,name,address,city,state,zip_code,balance);
    }
    else
    {
        a=true;
        while(accountNumber.length()!=10&&accountNumber!="-1")
        {
            cout<<"please reenter an account number which is 10 digits\n";
            in>>accountNumber;
        }
    }
    }
    return in;
}

ostream& operator<<(ostream& out,account& z)
{
    out<<z.get_account_number()<<"\n"
        <<z.get_title()<<"\n"
        <<z.get_address()<<"\n"
        <<z.get_city()<<"\n"
        <<z.get_state()<<"\n"
        <<z.get_zip_code()<<"\n"
        <<z.get_account_balance()<<"\n";
    return out;
}

Recommended Answers

All 20 Replies

>>Basically the please hit any key to enter doesn't show up in Visual C++.Thanks for help

Because you didn't tell it to say that. When you run the program from the IDE, that message appears because the IDE told it to. When you run the program from the command-line that message will not appears unless your program tells it to.

>>while(b.get_account_number()!=-1)
That is an infinite loop. The value returned by that method never changes.

but it supposed to change. I am still confused.

basically while b.get_account_number() value is changing. Atleast it seems to be. So to fix this would i need to create another constructor which takes in a value of accountNumber?

My guess is that line should be calling the overloaded >> operator while( infile >> b ) , but its only a guess because I don't know what your program is supposed to be doing.

the program is supposed to write the account object to the file until the account object data member accountNumber has a value of -1.

you naming of the file is confusing. infile is usually referencing a istream object. ostream objects are normally named outfile.

line 26: all that does is return the account number. There is nothing in that loop that will change the account number, thus its an infinite loop. line 38 calls the overloaded << operator, and it doesn't change the value of b either. So I have no idea from what you have posted how the values in b ever get changed. I assume the program should be calling the >> operator with an ifstream object somewhere. But that's never called in your program either.

i believe the problem is it must read all 7 data members.

the program is supposed to open a file and write from cin the account object. It is supposed to read an account object each time until the account number is -1.

you might want to call the >> operator in the while loop instead of get_account_number() so that the program can get the input data

while( cin >> b )
{
     if( b.get_account_number() == -1)
     {
              break;
     }
     // blabla

}

k thanks i will try that.

this causes the same problem as before. It doesn't show hit any key to enter.

//CS 310
/*Create a text file and input in certain data into the text
file*/
//Program one

#include <iostream>
#include <fstream>
#include "Account.h"
using namespace std;
int main()
{
    account b;
    char name1[256];
    cout<<"enter file name to be opened\n";
    cin>>name1;
    ofstream infile;
    infile.open(name1);
    if(!infile)
    {
        cout<<"File could not open\n";
        exit(1);
    }
    else
    {   
        while(cin>>b)
        {
            cout<<"account number is"<<b.get_account_number();
            if(b.get_account_balance()==-1)
            {
                cout<<"enter this condition?\n";
                break;
            }
            else
            {
                infile<<b;
            }
        }
    }

    return 0;
}

the infinite loop problem still remains.

i am an idiot for typing in accont balance instead of number.

nope problem still exists even if i change to b.get_account_number. :(.

HERE IS MOST RECENT CODE. DOESN'T SEEM TO ENTER IF CONDITION IN MY MAIN.

MAIN

//CS 310
/*Create a text file and input in certain data into the text
file*/
//Program one

#include <iostream>
#include <fstream>
#include "Account.h"
using namespace std;
int main()
{
    account b;
    char name1[256];
    bool c=true;
    cout<<"enter file name to be opened\n";
    cin>>name1;
    ofstream infile;
    infile.open(name1);
    if(!infile)
    {
        cout<<"File could not open\n";
        exit(1);
    }
    else
    {   
        while(c==true)
        {
            cin>>b;
            if(b.get_account_number()==-1)
            {
                cout<<"enter this condition?\n";
                c=false;
            }
            else
            {
                infile<<b;
                c=true;
            }
        }
    }

    return 0;
}

INTERFACE

//class used to create an account

#include <string>
using namespace std;
class account
{
private: 
    int account_Number;
    string name;
    string address;
    string city;
    string state;
    int zip_Code;
    double account_Balance;
public:

    //default account constructor
    account(int=0,  string="", string="", string="", string="", int=0, double=0);

    //sets the private variables above
    void set_account_number(size_t account_Number_Value);
    void set_title(string first_Last);
    void set_address(string location);
    void set_city(string county);
    void set_state(string loc);
    void set_zip_code(int zip);
    void set_account_balance(double user_balance);

    //gets the members value
    int get_account_number();
    string get_title();
    string get_address();
    string get_city();
    string get_state();
    int get_zip_code();
    double get_account_balance();

};

//overloaded operator to allow an object to be read in
istream& operator>>(istream&in,account& c);

//overload operator to an object to be read to the file
ostream& operator<<(ostream& out, account& z);

IMPLEMENATATION

#include "Account.h"
#include <iostream>
#include <iomanip>
using namespace std;

account::account(int accountNumberValue,string nameValue,string addressValue, string cityValue, string stateValue,
                 int zipCodeValue,double AccountBalanceValue)
{
    set_account_number(accountNumberValue);
    set_title(nameValue);
    set_address(addressValue);
    set_city(cityValue);
    set_state(stateValue);
    set_zip_code(zipCodeValue);
    set_account_balance(AccountBalanceValue);
}

void account::set_account_number(size_t account_Number_Value)
{
    account_Number=account_Number_Value;
}

void account::set_title(string first_Last)
{
    name=first_Last;
}

void account::set_address(string location)
{
    address=location;
}

void account::set_city(string county)
{
    city=county;
}

void account::set_state(string loc)
{
    state=loc;
}

void account::set_zip_code(int zip)
{
    zip_Code=zip;
}

void account::set_account_balance(double user_balance)
{
    account_Balance=user_balance;
}

int account::get_account_number()
{
    return account_Number;
}

string account::get_title()
{
    return name;
}

string account::get_address()
{
    return address;
}

string account::get_city()
{
    return city;
}

string account::get_state()
{
    return state;
}

int account::get_zip_code()
{
    return zip_Code;
}

double account::get_account_balance()
{
    return account_Balance;
}

istream& operator>>(istream&in,account& c)
{
    int number,zip_code,balance;
    string accountNumber;
    char name[256];
    char address[256];
    char city[256];
    char state[256];
    bool a =true;
    cout<<"enter account number\n";
    in>>accountNumber;
    while(a==true)
    {
        if(accountNumber.length()==10)
        {
            a=false;
            cout<<"enter your name\n";
            in.ignore();
            in.getline(name,256);
            cout<<"enter your address\n";
            in.getline(address,256);
            cout<<"enter your city\n";
            in.getline(city,256);
            cout<<"enter your state\n";
            in.getline(state,256);
            cout<<"enter your zip_code\n";
            in>>zip_code;
            cout<<"enter account balance\n";
            in>>balance;
            number=atoi(accountNumber.c_str());
            c=account(number,name,address,city,state,zip_code,balance);
        }
        else
        {
            if(accountNumber.length()!=10&&accountNumber!="-1")
            {
                a=true;
                cout<<"please reenter an account number which is 10 digits\n";
                in>>accountNumber;
            }   
        }
    }
    return in;
}

ostream& operator<<(ostream& out,account& z)
{
    out<<z.get_account_number()<<"\n"
        <<z.get_title()<<"\n"
        <<z.get_address()<<"\n"
        <<z.get_city()<<"\n"
        <<z.get_state()<<"\n"
        <<z.get_zip_code()<<"\n"
        <<z.get_account_balance()<<"\n";
    return out;
}

Lines 99 - 129 - This looks like an infinite loop to me if you enter "-1". Lines 101 to 119 are skipped since "-1" isn't 10 characters long. The "if" condition in line 122 isn't true since accountNumber is "-1". What code do you want to execute if accountNumber is equal to "-1"? Right now there is none, so you have an infinite while loop from line 99 to 129.

thanks. I asked teacher he told me same thing.

thanks. I asked teacher he told me same thing.

Guess you thought we didn't know what we were talking about :icon_eek:

no i thought you were refering to my int main(). My bad.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.