By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 833 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

skipping an element in an array

P: n/a
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[i]: " << a[i] <<endl;
cout<<" t2: " << t2 <<endl;
cout<<" before i: " << i <<endl;
i++;
a[i] = t2;
j = i;
cout<<" j: " << j <<endl;
cout<<" a[i]: " << a[i] <<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[i] = t1;
cout<<" before j: " << j <<endl;
cout<<" t1: " << t1 <<endl;
cout<<" i: " << i <<endl;
cout<<" a[i]: " << a[i] <<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[i]: " << a[i] << 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;
}
Jul 22 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a

"Ken" <le******@yah00.c0m> wrote in message
news:wj**************@news20.bellglobal.com...
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[i]: " << a[i] <<endl;
cout<<" t2: " << t2 <<endl;
cout<<" before i: " << i <<endl;
i++;
a[i] = t2;
j = i;
cout<<" j: " << j <<endl;
cout<<" a[i]: " << a[i] <<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[i] = t1;
cout<<" before j: " << j <<endl;
cout<<" t1: " << t1 <<endl;
cout<<" i: " << i <<endl;
cout<<" a[i]: " << a[i] <<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
Jul 22 '05 #2

P: n/a
Ken schrieb:
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.

--
Thomas

http://www.draig.de/LinkBar/
Jul 22 '05 #3

P: n/a
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" <jo*************@hotmail.com> wrote in message
news:2h************@uni-berlin.de...
"Ken" <le******@yah00.c0m> wrote in message
news:wj**************@news20.bellglobal.com...
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[i]: " << a[i] <<endl;
cout<<" t2: " << t2 <<endl;
cout<<" before i: " << i <<endl;
i++;
a[i] = t2;
j = i;
cout<<" j: " << j <<endl;
cout<<" a[i]: " << a[i] <<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[i] = t1;
cout<<" before j: " << j <<endl;
cout<<" t1: " << t1 <<endl;
cout<<" i: " << i <<endl;
cout<<" a[i]: " << a[i] <<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

Jul 22 '05 #4

P: n/a

"Ken" <le******@yah00.c0m> wrote in message
news:iz**************@news20.bellglobal.com...
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.


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
Jul 22 '05 #5

P: n/a
> >
THOSE HAS BEEN initialized in the private part of the class.


No they haven't.


Maybe you are confusing declaration with initialisation. They have been
declared, they have not been initialised.

john
Jul 22 '05 #6

P: n/a
Ken
crap, I am mixing those two, I will try that
ken
"John Harrison" <jo*************@hotmail.com> wrote in message
news:2h************@uni-berlin.de...

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


No they haven't.


Maybe you are confusing declaration with initialisation. They have been
declared, they have not been initialised.

john

Jul 22 '05 #7

P: n/a
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.


"John Harrison" <jo*************@hotmail.com> wrote in message
news:2h************@uni-berlin.de...

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


No they haven't.


Maybe you are confusing declaration with initialisation. They have been
declared, they have not been initialised.

john

Jul 22 '05 #8

P: n/a

"Ken" <le******@yah00.c0m> wrote in message
news:xr***************@news20.bellglobal.com...
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
Jul 22 '05 #9

P: n/a
Ken wrote:

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!

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.