C++ cin 一直跳过

发布于 2024-08-26 07:29:00 字数 6769 浏览 6 评论 0原文

我的程序有问题。当我运行它时,它会询问用户专辑、标题,但随后它只是退出循环,而不询问价格和销售税。有什么想法吗?

这是示例运行

Discounts effective for September 15, 2010
Classical   8%
Country 4%
International   17%
Jazz    0%
Rock    16%
Show    12%
Are there more transactions? Y/N
y
Enter Artist of CD: 
Sevendust
Enter Title of CD: 
Self titled
Enter Genre of CD: 
Rock
enter price
Are there more transactions? Y/N
Thank you for shopping with us!

程序代码:

#include <iostream>

#include <string>



using namespace std;



int counter = 0;

string discount_tiles[] = {"Classical", "Country", "International", "Jazz", "Rock", "Show"};

int discount_amounts[] = {8, 4, 17, 0, 16, 12, 14};

string date = "September 15, 2010";



// Array Declerations

    //Artist array

    char** artist = new char *[100];        

    //Title array

    char** title = new char *[100]; 

    //Genres array

    char** genres = new char *[100];

    //Price array

    double* price[100]; 

    //Discount array

    double* tax[100];

    // sale price array

    double* sale_price[100];    

    //sale tax array

    double* sale_tax[100];  

    //cash price array

    double* cash_price[100];    



//Begin Prototypes

char* getArtist();

char* getTitle();

char* getGenre();

double* getPrice();

double* getTax();

unsigned int* AssignDiscounts();

void ReadTransaction (char ** artist, char ** title, char ** genre, float ** cash, float & taxrate, int albumcount);

void computesaleprice();





bool AreThereMore ();



//End Prototypes



bool areThereMore ()

{   

    char answer;

    cout << "Are there more transactions? Y/N" << endl;

    cin >> answer;

    if (answer =='y' || answer =='Y')

        return true;

    else 

        return false;

}



char* getArtist()

{

    char * artist= new char [100];

    cout << "Enter Artist of CD: " << endl;

    cin.getline(artist,100);

    cin.ignore();

    return artist;

}



char* getTitle()

{

    char * title= new char [100];

    cout << "Enter Title of CD: " << endl;

    cin.getline(title,100);

    cin.ignore();

    return title;

}



char* getGenre()

{

    char * genre= new char [100];

    cout << "Enter Genre of CD: " << endl;

    cin.getline(genre,100);

    cin.ignore();

    return genre;

}



double* getPrice()

{   

    //double* price = new double();

    //cout << "Enter Price of CD: " << endl;

    //cin >> *price;



    //return price;



    double p = 0.0;

    cout<< "enter price" << endl;

    cin >> p;

    cin.ignore();

    double* pp = &p;

    return pp;

}



double* getTax()

{

    double* tax= new double();

    cout << "Enter local sales tax: " << endl;

    cin >> *tax;



    return tax;

}



int findDiscount(string str){



    if(str.compare(discount_tiles[0]) == 0)

        return discount_amounts[0];

    else if(str.compare(discount_tiles[0]) == 0)

        return discount_amounts[1];

    else if(str.compare(discount_tiles[0]) == 0)

        return discount_amounts[2];

    else if(str.compare(discount_tiles[0]) == 0)

        return discount_amounts[3];

    else if(str.compare(discount_tiles[0]) == 0)

        return discount_amounts[4];

    else if(str.compare(discount_tiles[0]) == 0)

        return discount_amounts[5];

    else{

        cout << "Error in findDiscount function" << endl;

        return 0;

    }

}



void computesaleprice()

{

    /** fill in array for all purchases **/

    for( int i=0; i<=counter; i++){

        double temp = *price[i];

        temp -= findDiscount(genres[i]);

        double* tmpPntr = new double();

        tmpPntr = &temp;

        sale_price[i] = tmpPntr;

        delete(&temp);

        delete(tmpPntr);

    }

}











void printDailyDiscounts(){

    cout << "Discounts effective for " << date << endl;

    for(int i=0; i < 6; i++){

        cout << discount_tiles[i] << "\t" << discount_amounts[i] << "%" << endl;

    }

}



//Begin Main

int main ()

{







    for( int i=0; i<100; i++){

        artist[i]=new char [100];

        title[i]=new char [100];

        genres[i]=new char [100];       

        price[i] = new double(0.0);

        tax[i] = new double(0.0);

    }



    // End Array Decleration



    printDailyDiscounts();



    bool flag = true;   

    while(flag == true){

        if(areThereMore() == true){

            artist[counter] = getArtist();

            title[counter] = getTitle();

            genres[counter] = getGenre();

            price[counter] = getPrice();

            //tax[counter] = getTax();          

            //counter++;

            flag = true;

        }

        else {

            flag = false;

        }

    }





    //compute sale prices

    //computesaleprice();













    cout << "Thank you for shopping with us!" << endl;



    return 0;

}

//End Main













/**

void ReadTransaction (char ** artist, char ** title, char ** genre, float ** cash, float & taxrate, int albumcount)

{

    strcpy(artist[albumcount],getArtist());

    strcpy(title[albumcount],getTitle());

    strcpy(genre[albumcount],getGenre());

    //cash[albumcount][0]=computesaleprice();???????

    //taxrate=getTax;??????????????





}

* 

* */







unsigned int * AssignDiscounts()

{

    unsigned int * discount = new unsigned int [7];

    cout << "Enter Classical Discount: " << endl;

    cin >> discount[0];

    cout << "Enter Country Discount: " << endl;

    cin >> discount[1];

    cout << "Enter International Discount: " << endl;

    cin >> discount[2];

    cout << "Enter Jazz Discount: " << endl;

    cin >> discount[3];

    cout << "Enter Pop Discount: " << endl;

    cin >> discount[4];

    cout << "Enter Rock Discount: " << endl;

    cin >> discount[5];

    cout << "Enter Show Discount: " << endl;

    cin >> discount[6];

    return discount;

}



/**

char ** AssignGenres ()

{

    char ** genres = new char * [7];

    for (int x=0;x<7;x++)

        genres[x] = new char [20];

    strcpy(genres [0], "Classical");

    strcpy(genres [1], "Country");

    strcpy(genres [2], "International");

    strcpy(genres [3], "Jazz");

    strcpy(genres [4], "Pop");

    strcpy(genres [5], "Rock");

    strcpy(genres [6], "Show");

    return genres;

}

**/







float getTax(float taxrate)

{

    cout << "Please enter store tax rate: " << endl;

    cin >> taxrate;

    return taxrate;

}

I am having problems with my program. WHen I run it, it asks the user for the album, the title, but then it just exits the loop without asking for the price and the sale tax. Any ideas what's going on?

This is a sample run

Discounts effective for September 15, 2010
Classical   8%
Country 4%
International   17%
Jazz    0%
Rock    16%
Show    12%
Are there more transactions? Y/N
y
Enter Artist of CD: 
Sevendust
Enter Title of CD: 
Self titled
Enter Genre of CD: 
Rock
enter price
Are there more transactions? Y/N
Thank you for shopping with us!

Program code:

#include <iostream>

#include <string>



using namespace std;



int counter = 0;

string discount_tiles[] = {"Classical", "Country", "International", "Jazz", "Rock", "Show"};

int discount_amounts[] = {8, 4, 17, 0, 16, 12, 14};

string date = "September 15, 2010";



// Array Declerations

    //Artist array

    char** artist = new char *[100];        

    //Title array

    char** title = new char *[100]; 

    //Genres array

    char** genres = new char *[100];

    //Price array

    double* price[100]; 

    //Discount array

    double* tax[100];

    // sale price array

    double* sale_price[100];    

    //sale tax array

    double* sale_tax[100];  

    //cash price array

    double* cash_price[100];    



//Begin Prototypes

char* getArtist();

char* getTitle();

char* getGenre();

double* getPrice();

double* getTax();

unsigned int* AssignDiscounts();

void ReadTransaction (char ** artist, char ** title, char ** genre, float ** cash, float & taxrate, int albumcount);

void computesaleprice();





bool AreThereMore ();



//End Prototypes



bool areThereMore ()

{   

    char answer;

    cout << "Are there more transactions? Y/N" << endl;

    cin >> answer;

    if (answer =='y' || answer =='Y')

        return true;

    else 

        return false;

}



char* getArtist()

{

    char * artist= new char [100];

    cout << "Enter Artist of CD: " << endl;

    cin.getline(artist,100);

    cin.ignore();

    return artist;

}



char* getTitle()

{

    char * title= new char [100];

    cout << "Enter Title of CD: " << endl;

    cin.getline(title,100);

    cin.ignore();

    return title;

}



char* getGenre()

{

    char * genre= new char [100];

    cout << "Enter Genre of CD: " << endl;

    cin.getline(genre,100);

    cin.ignore();

    return genre;

}



double* getPrice()

{   

    //double* price = new double();

    //cout << "Enter Price of CD: " << endl;

    //cin >> *price;



    //return price;



    double p = 0.0;

    cout<< "enter price" << endl;

    cin >> p;

    cin.ignore();

    double* pp = &p;

    return pp;

}



double* getTax()

{

    double* tax= new double();

    cout << "Enter local sales tax: " << endl;

    cin >> *tax;



    return tax;

}



int findDiscount(string str){



    if(str.compare(discount_tiles[0]) == 0)

        return discount_amounts[0];

    else if(str.compare(discount_tiles[0]) == 0)

        return discount_amounts[1];

    else if(str.compare(discount_tiles[0]) == 0)

        return discount_amounts[2];

    else if(str.compare(discount_tiles[0]) == 0)

        return discount_amounts[3];

    else if(str.compare(discount_tiles[0]) == 0)

        return discount_amounts[4];

    else if(str.compare(discount_tiles[0]) == 0)

        return discount_amounts[5];

    else{

        cout << "Error in findDiscount function" << endl;

        return 0;

    }

}



void computesaleprice()

{

    /** fill in array for all purchases **/

    for( int i=0; i<=counter; i++){

        double temp = *price[i];

        temp -= findDiscount(genres[i]);

        double* tmpPntr = new double();

        tmpPntr = &temp;

        sale_price[i] = tmpPntr;

        delete(&temp);

        delete(tmpPntr);

    }

}











void printDailyDiscounts(){

    cout << "Discounts effective for " << date << endl;

    for(int i=0; i < 6; i++){

        cout << discount_tiles[i] << "\t" << discount_amounts[i] << "%" << endl;

    }

}



//Begin Main

int main ()

{







    for( int i=0; i<100; i++){

        artist[i]=new char [100];

        title[i]=new char [100];

        genres[i]=new char [100];       

        price[i] = new double(0.0);

        tax[i] = new double(0.0);

    }



    // End Array Decleration



    printDailyDiscounts();



    bool flag = true;   

    while(flag == true){

        if(areThereMore() == true){

            artist[counter] = getArtist();

            title[counter] = getTitle();

            genres[counter] = getGenre();

            price[counter] = getPrice();

            //tax[counter] = getTax();          

            //counter++;

            flag = true;

        }

        else {

            flag = false;

        }

    }





    //compute sale prices

    //computesaleprice();













    cout << "Thank you for shopping with us!" << endl;



    return 0;

}

//End Main













/**

void ReadTransaction (char ** artist, char ** title, char ** genre, float ** cash, float & taxrate, int albumcount)

{

    strcpy(artist[albumcount],getArtist());

    strcpy(title[albumcount],getTitle());

    strcpy(genre[albumcount],getGenre());

    //cash[albumcount][0]=computesaleprice();???????

    //taxrate=getTax;??????????????





}

* 

* */







unsigned int * AssignDiscounts()

{

    unsigned int * discount = new unsigned int [7];

    cout << "Enter Classical Discount: " << endl;

    cin >> discount[0];

    cout << "Enter Country Discount: " << endl;

    cin >> discount[1];

    cout << "Enter International Discount: " << endl;

    cin >> discount[2];

    cout << "Enter Jazz Discount: " << endl;

    cin >> discount[3];

    cout << "Enter Pop Discount: " << endl;

    cin >> discount[4];

    cout << "Enter Rock Discount: " << endl;

    cin >> discount[5];

    cout << "Enter Show Discount: " << endl;

    cin >> discount[6];

    return discount;

}



/**

char ** AssignGenres ()

{

    char ** genres = new char * [7];

    for (int x=0;x<7;x++)

        genres[x] = new char [20];

    strcpy(genres [0], "Classical");

    strcpy(genres [1], "Country");

    strcpy(genres [2], "International");

    strcpy(genres [3], "Jazz");

    strcpy(genres [4], "Pop");

    strcpy(genres [5], "Rock");

    strcpy(genres [6], "Show");

    return genres;

}

**/







float getTax(float taxrate)

{

    cout << "Please enter store tax rate: " << endl;

    cin >> taxrate;

    return taxrate;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与君绝 2024-09-02 07:29:00

您正在从 getPrice 返回一个指向局部变量的指针:

double* getPrice()
{   
    double p = 0.0;
    cout<< "enter price" << endl;
    cin >> p;
    cin.ignore();
    double* pp = &p;
    return pp;
}

由于当您从函数返回时 p 超出了范围(即被销毁),您会得到一个悬空指针,这会导致未定义的行为。

不需要销售税,因为对 getTax() 的调用已被注释掉。

You are returning a pointer to a local variable from getPrice:

double* getPrice()
{   
    double p = 0.0;
    cout<< "enter price" << endl;
    cin >> p;
    cin.ignore();
    double* pp = &p;
    return pp;
}

Since p goes out of scope (i.e. gets destroyed) when you return from the function, you get a dangling pointer, which results in undefined behaviour.

Sale tax is not asked for because the call to getTax() is commented out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文