skipping an element in an array

K

Ken

I have a small program , wi tha menu (only option 1,2 and 6 are working
for now)
Something weird is happening, my array works to input all the data the user
puts in, but whebn I choose option 2, display, it always gives me 4202717
for the element number 1 in the array, While 0,2,3 .. are all ok.
It seems to be skipping number 1 in the array.
anyone ?
problem is in createTime() function

I am using visual c++
ken


#include <iostream>
#include <conio.h>
using namespace std;

class timeChange
{
public:
void createTime();
void display1();

private:
int t1, t2, i, j, a[] ;
};

inline void timeChange::createTime()
{

if (t1 >0)
{
cout<<" Enter a number 1: " <<endl;
cin>> t2;
i=0;
i = i + j;
do {
cout<<" before j: " << j <<endl;
cout<<" a: " << a <<endl;
cout<<" t2: " << t2 <<endl;
cout<<" before i: " << i <<endl;
i++;
a = t2;
j = i;
cout<<" j: " << j <<endl;
cout<<" a: " << a <<endl;
cout<<" t2: " << t2 <<endl;
cout<<" i: " << i <<endl;
}
while (t1 < 0);

}

else
{
cout<<" Enter a number 2: " <<endl;
cin>> t1;
i =0;
do {
a = t1;
cout<<" before j: " << j <<endl;
cout<<" t1: " << t1 <<endl;
cout<<" i: " << i <<endl;
cout<<" a: " << a <<endl;
cout<<" before i++: " << i <<endl;
i++;
j = i;

cout<<" j: " << j <<endl;
cout<<" t1: " << t1 <<endl;
cout<<" i: " << i <<endl;
}
while (t1 < 0);


} // end else

} //end void function
inline void timeChange::display1()
{

cout<<" Here it is: " << t1 <<endl;
for (int i=0; i<= j; i++)

{
cout<<"a: " << a << endl;
cout<<"i: " << i << endl;
}

}

/*inline void timeChange::diffTime()
inline void timeChange::substracTime()
inline void timeChange::addTime()
inline void timeChange::validateTime()
*/


void menu() //showing the menu for the 4 options
{ timeChange time;
int choice;

do {
cout << endl << endl;
cout << " Time Management System "<<endl;
cout << "============================================== "<<endl;
cout << " 1: Create a new time object "<<endl;
cout << " 2: Display all time object "<<endl;
cout << " 3: Calculate the difference between two times "<<endl;
cout << " 4: Substrat a number of seconds from a time "<<endl;
cout << " 5: Add a number of seconds to a time "<<endl;
cout << " 6: Quit "<<endl;
cout << "============================================== "<<endl;

cout << " Your choice please: ";
cin >> choice;

switch (choice)
{
case 1:
time.createTime();
//Calling function to convert seconds into hours
break;
case 2:
time.display1(); //Calling function to convert tome to seconds
break;
/* case 3:
time.diffTime();
break;
case 4:
time.substracTime(); //Calling function to subtract a time from seconds
break;
case 5:
time.addTime(); //Calling function to subtract a time from seconds
break;
*/ case 6: cout<<"Thank you for having used this system, Bye Bye!!!";
break;

default: cout<<"Error: Invalid option, Please try again" ;
}
} while (choice != 6);

}// end function menu


int main()
{
timeChange time;
menu();


getch();
return 0;
}
 
J

John Harrison

Ken said:
I have a small program , wi tha menu (only option 1,2 and 6 are working
for now)
Something weird is happening, my array works to input all the data the user
puts in, but whebn I choose option 2, display, it always gives me 4202717
for the element number 1 in the array, While 0,2,3 .. are all ok.
It seems to be skipping number 1 in the array.
anyone ?
problem is in createTime() function

I am using visual c++
ken


#include <iostream>
#include <conio.h>
using namespace std;

class timeChange
{
public:
void createTime();
void display1();

private:
int t1, t2, i, j, a[] ;

a[] is not legal C++. I've no idea what visual c++ makes of it, but it isn't
right. If you want to declare an array in C++ you must say how big you want
it to be.
};

inline void timeChange::createTime()
{

if (t1 >0)

t1 is unintialised at this point
{
cout<<" Enter a number 1: " <<endl;
cin>> t2;
i=0;
i = i + j;

j is uninitialised at this point
do {
cout<<" before j: " << j <<endl;
cout<<" a: " << a <<endl;
cout<<" t2: " << t2 <<endl;
cout<<" before i: " << i <<endl;
i++;
a = t2;
j = i;
cout<<" j: " << j <<endl;
cout<<" a: " << a <<endl;
cout<<" t2: " << t2 <<endl;
cout<<" i: " << i <<endl;
}
while (t1 < 0);

}

else
{
cout<<" Enter a number 2: " <<endl;
cin>> t1;
i =0;
do {
a = t1;
cout<<" before j: " << j <<endl;
cout<<" t1: " << t1 <<endl;
cout<<" i: " << i <<endl;
cout<<" a: " << a <<endl;
cout<<" before i++: " << i <<endl;
i++;
j = i;

cout<<" j: " << j <<endl;
cout<<" t1: " << t1 <<endl;
cout<<" i: " << i <<endl;
}
while (t1 < 0);


} // end else

} //end void function


You shouldn't use variables before you have given them values.

john
 
T

Thomas J. Gritzan

Ken said:
problem is in createTime() function

[...]

class timeChange
{
public:
void createTime();
void display1();

private:
int t1, t2, i, j, a[] ;
};

inline void timeChange::createTime()
{

if (t1 >0)

First, you should initialize the variables of the class before using them.
Write a constructor and set t1,t2,i,j to zero or whatever you like.
If you don't do so, the variables contain "random" numbers.

Second, "int a[]" is nothing. It is a declaration of an int-array with zero
elements. It does not grow automaticaly when you access an index that is out
of bound.

Maybe you should read a good book about C++ programming or search the web
for an online tutorial.
 
K

Ken

t1 is unintialised at this point
{
cout<<" Enter a number 1: " <<endl;
cin>> t2;
i=0;
i = i + j;

j is uninitialised at this point


THOSE HAS BEEN initialized in the private part of the class.
Second, if I give them a value at the beginning of the function, how can
my incrementation work ???


ken

John Harrison said:
Ken said:
I have a small program , wi tha menu (only option 1,2 and 6 are working
for now)
Something weird is happening, my array works to input all the data the user
puts in, but whebn I choose option 2, display, it always gives me 4202717
for the element number 1 in the array, While 0,2,3 .. are all ok.
It seems to be skipping number 1 in the array.
anyone ?
problem is in createTime() function

I am using visual c++
ken


#include <iostream>
#include <conio.h>
using namespace std;

class timeChange
{
public:
void createTime();
void display1();

private:
int t1, t2, i, j, a[] ;

a[] is not legal C++. I've no idea what visual c++ makes of it, but it isn't
right. If you want to declare an array in C++ you must say how big you want
it to be.
};

inline void timeChange::createTime()
{

if (t1 >0)

t1 is unintialised at this point
{
cout<<" Enter a number 1: " <<endl;
cin>> t2;
i=0;
i = i + j;

j is uninitialised at this point
do {
cout<<" before j: " << j <<endl;
cout<<" a: " << a <<endl;
cout<<" t2: " << t2 <<endl;
cout<<" before i: " << i <<endl;
i++;
a = t2;
j = i;
cout<<" j: " << j <<endl;
cout<<" a: " << a <<endl;
cout<<" t2: " << t2 <<endl;
cout<<" i: " << i <<endl;
}
while (t1 < 0);

}

else
{
cout<<" Enter a number 2: " <<endl;
cin>> t1;
i =0;
do {
a = t1;
cout<<" before j: " << j <<endl;
cout<<" t1: " << t1 <<endl;
cout<<" i: " << i <<endl;
cout<<" a: " << a <<endl;
cout<<" before i++: " << i <<endl;
i++;
j = i;

cout<<" j: " << j <<endl;
cout<<" t1: " << t1 <<endl;
cout<<" i: " << i <<endl;
}
while (t1 < 0);


} // end else

} //end void function


You shouldn't use variables before you have given them values.

john
 
J

John Harrison

Ken said:
t1 is unintialised at this point


j is uninitialised at this point


THOSE HAS BEEN initialized in the private part of the class.

No they haven't.
Second, if I give them a value at the beginning of the function, how can
my incrementation work ???

You write a constructor, and give them a value there. I think you need to
buy a book on C++ and do some studying.

john
 
K

Ken

I did bought a book recently on the subject.
But all the books and tutorial I have seen so far always deals with data
already inserted in the programs wich do not ask anything from the user.
The data I want to treat would be in a >>cin , so I do not see how I can
initialized those values in a constructor.
 
J

John Harrison

Ken said:
I did bought a book recently on the subject.
But all the books and tutorial I have seen so far always deals with data
already inserted in the programs wich do not ask anything from the user.
The data I want to treat would be in a >>cin , so I do not see how I can
initialized those values in a constructor.

It doesn't change the fact that you are using t1 and j before you have
assigned them values.

Maybe you don't want to give them values in a constructor but you have to
given them values *somewhere* before you start using them.

john
 
K

Karl Heinz Buchegger

Ken said:
I did bought a book recently on the subject.
But all the books and tutorial I have seen so far always deals with data
already inserted in the programs wich do not ask anything from the user.
The data I want to treat would be in a >>cin , so I do not see how I can
initialized those values in a constructor.

Then ask yourself.
In (code posted by yourself)

#include <iostream>
#include <conio.h>
using namespace std;

class timeChange
{
public:
void createTime();
void display1();

private:
int t1, t2, i, j, a[] ;
};

inline void timeChange::createTime()
{

if (t1 >0)
{
cout<<" Enter a number 1: " <<endl;
cin>> t2;
i=0;
i = i + j;

What is the value of t1 right after the function starts executing?
Nobody knows, it hasn't been given a value right now. Yet you
use that variable in the comparison in the very first statement
in this function.

Even if some of the values are enterd by the user, it is *still*
a good idea to use the help of a constructor to give them
some defined values.

class timeChange
{
public:
timeChange() { t1 = 0;
t2 = 0;
i = 0;
j = 0;
}

....

so that whenever a timeChange object comes into existence, it is
*guaranteed* that those member variables have some defined values.
Now when function createTime starts execution, we *know* that t1
has a value of 0!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top