here is my outline of code:

void apple()
{
cin >> price[i];
cout << price[i];        //give out correct value
}
void orange()
{
cin >> price2;
cout << price[i];       //mess value (like 65534 or 9460301)
}

int main()
{
string *fruit;
int *price;
fruit = new string[n]
price = new int[n]

for(int i=0,i<n){
cin >> fruit[i];
if (fruit[i]=="apple"){
apple();}
if (fruit[i]=="orange"){
orange();}
i++
}

}

just roughly outline,
i use pointer for apple but orange doesnt,
why i can get correct value from "cout <<" in apple,
but i get mess value from orange??

Recommended Answers

All 8 Replies

apple and orange should be enclosed in double quotes..

"orange"
"apple"

yes, typing wrong here,
my code already have double quoted

The code you posted cannot possibly compile for several reasons, including:

1) You never defined the variables n or price2.

2) The variables price and price2 do not have definitions in scope in the functions apple and orange.

If you post code that does not compile, and you complain that the code does not do what you want when you run it, you are essentially telling everyone that the code you posted is not the code you ran--because you could not have run it as it did not compile.

Why should we bother trying to debug code that we know is different from the code with which you claim to be having problems?

I can't post my original code as it is too long,
and i am sure that i have no coding mistake in the original code(missing out definitions, wrong placing etc.)
so i am here to ask if there is technical problem, the above is a simple verion of the original code

One observation:

string* sp;
sp = new string[n];     // every element of the array is initialized to a null string
int* ip;
ip = new int[n];        // the elements of the array are not initialized

This behavior might explain your problem.

If it doesn't, there isn't anything more I can say. You posted code that doesn't work, and say "My real code is just like this, except that it's perfect, and I won't show it to you because it's too long; but I want you to tell me what's wrong with it."

Sorry, but that kind of magic is above my pay grade.

1.
      string* sp;
   2.
      sp = new string[n]; // every element of the array is initialized to a null string
   3.
      int* ip;
   4.
      ip = new int[n]; // the elements of the array are not initialized

i can't understand...

//my main
int sizeOfArray;

int main()
{
int i;
int *num1, *num2, *num3, *num4, *num5;
string *opening, *ending;
cin >> sizeOfArray;
command = new string [sizeOfArray];
    opening= new int [sizeOfArray];
    num1= new int [sizeOfArray];
    num2= new int [sizeOfArray];
    num3= new int [sizeOfArray];
    num4= new int [sizeOfArray];
    num5 = new int [sizeOfArray];
    ending= new string [sizeOfArray];

for(i= 0; i< sizeOfArray;){
    cin >> opening[i];
if (opening[i] == "hello"){
hello(_____);}
if (opening[i] == "world"){
world(_____);}
i++;}
//void hello
void hello(______)
{
cin >> num1 [i] >>num2 [i] >>num3[i] >>num4[i] >>num5[i];
getline(cin,ending[i];
}
//void world
void select()
{
int a,b,c,d;
cin >> a >> b >> c >> d;

for(int k= 0; k< sizeOfArray; k++){
cout << num1 [k] << endl;           // <-- here, cannot get correct value of num1 that i input before
}}

that what i can show you, it is almost 90% same as the original code,
any help is appreciated~

below is model input:
2
hello 1 2 3 4 5 bye
world 7 7 7 7
(the output is some meaningless number)

please help...~~
this is urgent:(:(:(

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.