-4

I am new to C++ and still learning it.I did an exercise on book and everything seems right in my program,but it will just crash when showing the inputs that I entered.I tried to ran it on different compilers and computers,the results were the same.Hope someone will help me thank you.And here is the program.

#include<iostream>                      
using namespace std;

int main()                  
{

    string name;
    char code[1];
    float price;
    int quantity;

    cout<<"Enter an item name : "<<endl;
    getline(cin,name);

    cout<<"Enter the item code : "<<endl;
    cin>>code;

    cout<<"Enter the price per unit : "<<endl;
    cin>>price;

    cout<<"Enter the quanlity : "<<endl;
    cin>>quantity;

    cout<<"You entered the item name : "<<name<<endl
        <<"You entered the item code : "<<code<<endl
        <<"You entered the item price : "<<"RM"<<price<<endl
        <<"You entered the item quantity: "<<quantity<<" unit"<<endl
        <<"The total cost : "<<"RM"<<(quantity*price)<<endl;    
    return 0;
}

and this is the output:

Ahmad Khan
  • 2,655
  • 19
  • 25
FT TEH
  • 11
  • 2
  • change `code[1]` to `code` ? – apple apple Oct 07 '16 at 16:01
  • I'd change `char[1]` in a single `char`, otherwise `std::cin` will insert a `\0` at `[1]` producing an UB – BiagioF Oct 07 '16 at 16:05
  • 3
    The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should \[edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Oct 07 '16 at 16:06
  • @appleapple Yes,it worked.but how do I specific that only one character is used.If using char code,and the key in more than one char,than the outputs will be not right. – FT TEH Oct 07 '16 at 16:07
  • @FTTEH `ignore()` other items is an option – apple apple Oct 07 '16 at 16:08

1 Answers1

1

char code[1]; Arrays are treated like pointers (they "decay") when passed to functions. More on Arrays decaying here: What is array decaying?

cin>>code; >>is actually a function, so code is seen as a pointer to char. It treats a pointer to char as though it is a c-style string and attempts to null terminate it. Sadly there is only room for one character and no room for the null terminator, so the null terminator is written to memory that is not owned by the program.

If the program survives that, "You entered the item code : "<<code<<endl. << also treats a pointer to char as though it is a c-style string and attempts to write until it finds the null terminator, reading past the end of the array and from invalid memory and who knows where it will finally find a null character and stop.

Solution:

If you only want one char, define code to be only one char with

char code;
Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54