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;
} 9 1953
"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
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/
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
"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
> > 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
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
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
"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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: maciej |
last post by:
I got an array that consists of elements that are arrays also. Now I
wish I could add an element to the middle of it. Let mi give you an
example:
array (
- array(1,15,apple),
-...
|
by: JackM |
last post by:
I'm having a brain freeze on this. I have a script where I am reading
the contents of a file into an array. I have that up and working with no
problem. Then I'm exploding the array using
while...
|
by: Chuck Anderson |
last post by:
I have a function with 7 inputs. The last three have default values.
I want to call that function specifying the first four, skip two and
then specify the last.
I thought I could write this...
|
by: Noah Spitzer-Williams |
last post by:
Hello guys,
I would like to do something seemingly simple: find out if an
element in an array that is passed to my function exists.
I used to think I could just do: if (arr) ...
However, if...
|
by: Geoff Bennett |
last post by:
While parsing an XML document, my TextReader instance skips nodes. For
example, in this fragment:
<Person Sex="Male" FirstHomeBuyer="No" YearsInCurrentProfession="14">
<RelatedEntityRef...
|
by: Luke Wu |
last post by:
Hello,
I'm having some problems understanding 2 dimensional arrays. My
problem relates to the following code:
#include <stdio.h>
#define M 3
#define N 3
|
by: Rob Griffiths |
last post by:
Can anyone explain to me the difference between an element type and a
component type?
In the java literature, arrays are said to have component types, whereas
collections from the Collections...
|
by: NickPomp |
last post by:
Hi,
I have to write a slide puzzle program for class. I have the program finished and working except that I can not get the blank space to print out. I wrote code that would find the number I used...
|
by: Gustaf |
last post by:
Hi all,
Just for fun, I'm working on a script to count the number of lines in source files. Some lines are auto-generated (by the IDE) and shouldn't be counted. The auto-generated part of files...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |