跳过数组中的元素 [英] skipping an element in an array

查看:101
本文介绍了跳过数组中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小程序,没有菜单(只有选项1,2和6正在工作

现在)

奇怪的事情正在发生,我的阵列工作到输入用户输入的所有数据

输入,但是我选择选项2,显示,它总是给我4202717

为数组中的元素编号1,而0 ,2,3 ..都没关系。

它似乎是在数组中跳过1号。

任何人?

问题在于createTime()函数


我使用的是visual c ++

ken

#include< iostream>

#include< conio.h>

使用命名空间std;


class timeChange

{

public:

void createTime();

void display1();


private:

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

};


inline void timeChange :: createTime()

{


if(t1> 0)

{

cout<<""输入数字1: << endl;

cin>> t2;

i = 0;

i = i + j;

do {

cout<<""在j之前: << j<< endl;

cout<<" a [i]: << a [i]<< endl;

cout<<"" t2: << t2<< endl;

cout<<"在我之前: << i<< endl;

i ++;

a [i] = t2;

j = i;

COUT<<" j: << j<< endl;

cout<<" a [i]: << a [i]<< endl;

cout<<"" t2: << t2<< endl;

cout<<"我: <<我<< endl;

}

而(t1< 0);


}


else

{

cout<<""输入数字2: << endl;

cin>> t1;

i = 0;

do {

a [i] = t1;

cout<<" ;在j之前: << j<< endl;

cout<<" t1: << t1<< endl;

cout<<"我: <<我<< endl;

cout<<" a [i]: << a [i]<< endl;

cout<<""在i ++之前: << i<< endl;

i ++;

j = i;


cout<<" j: << j<< endl;

cout<<" t1: << t1<< endl;

cout<<"我: <<我<< endl;

}

而(t1< 0);

} //结束其他


} //结束无效功能

内联void timeChange :: display1()

{


COUT<<"这是: << t1<< endl;

for(int i = 0; i< = j; i ++)


{

cout<<" a [i]:" << a [i]<< endl;

cout<<" i:" << i<<结束;

}


}


/ * inline void timeChange :: diffTime()

inline void timeChange :: substracTime()

inline void timeChange :: addTime()

inline void timeChange :: validateTime()

* /

void menu()//显示4个选项的菜单

{timeChange time;

int choice;


做{

cout<< endl<< endl;

cout<< "时间管理系统<< endl;

cout<< ============================================= = << endl;

cout<< " 1:创建一个新的时间对象<< endl;

cout<< " 2:显示所有时间对象<< endl;

cout<< " 3:计算两次之间的差值<< endl;

cout<< " 4:从时间<<<< endl;

cout<<<< " 5:在一段时间内添加一个秒数<<< endl;

cout<< " 6:退出"<< endl;

cout<< ============================================= = << endl;


cout<< "您的选择请:;

cin>>选择;


开关(选择)

{

案例1:

time.createTime( );

//调用函数将秒转换为小时

休息;

案例2:

时间。 display1(); //调用函数将tome转换为秒

break;

/ * case 3:

time.diffTime();

休息;

案例4:

time.substracTime(); //调用函数从秒中减去时间

break;

case 5:

time.addTime(); //调用函数从秒中减去时间

break;

* / case 6:cout<<"谢谢你使用过这个系统,Bye Bye! !!  $

休息;


默认值:cout<<"错误:无效选项,请再试一次 ;

}

} while(选择!= 6);


} //结束功能菜单

int main()

{

timeChange time;

menu();

getch() ;

返回0;

}

解决方案



"肯" <乐****** @ yah00.c0m>在留言中写道

新闻:wj ************** @ news20.bellglobal.com ...

我有一个小程序,wi tha菜单(目前只有选项1,2和6正在工作)
发生了一些奇怪的事情,我的数组工作输入
用户输入的所有数据,但是我是选择选项2,显示,它总是给我4202717
数组中的元素编号1,而0,2,3 ..都没关系。
它似乎是跳过数组中的数字1 。
任何人?
问题在于createTime()函数

我正在使用visual c ++
ken

#include< iostream>
#include< conio.h>
使用命名空间std;

类timeChange
{
公开:
void createTime();
void display1();

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


a []不是合法的C ++。我不知道visual c ++是什么造成的,但它并不是正确的。如果你想用C ++声明一个数组,你必须说出你想要多大的数量



};

内联void timeChange: :createTime()
{

if(t1> 0)


t1此时未实现

{
cout<<<"输入数字1: << endl;
cin>> t2;
i = 0;
i = i + j;


j此时尚未初始化

do {
cout<<"在j之前: << j<< endl;
cout<<" a [i]: << a [i]<< endl;
cout<<" t2: << t2<< endl;
cout<<"在我之前: << i<< endl;
i ++;
a [i] = t2;
j = i;
cout<<" j: << j<< endl;
cout<<" a [i]: << a [i]<< endl;
cout<<" t2: << t2<< endl;
cout<<"我: <<我<< endl;
}
而(t1< 0);

}


{
cout<<"输入数字2: << endl;
cin>> t1;
i = 0;
做{
a [i] = t1;
cout<<<"在j之前: << j<< endl;
cout<<" t1: << t1<< endl;
cout<<"我: <<我<< endl;
cout<<" a [i]: << a [i]<< endl;
cout<<"在i ++之前: << i<< endl;
i ++;
j = i;

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

} //结束其他

} //结束无效函数




在给出值之前你不应该使用变量。


john


Ken schrieb:

问题在于createTime()函数

[...]

类timeChange
{
公开:
void createTime();
void display1();

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

内联void timeChange :: createTime()

if(t1> 0)




首先,你应该在使用它们之前初始化类的变量。

写一个构造函数并将t1,t2,i,j设置为零或者你喜欢什么。

如果你不这样做,变量包含随机和随机。数字。


其次," int a []"没什么。它是一个int数组的声明,其元素为零

。当你访问索引时,它不会自动增长

绑定。


也许你应该读一本关于C ++编程或搜索网络的好书

的在线教程。


-

Thomas

http://www.draig.de/LinkBar/

此时t1是未初始化的

{
cout<<"输入数字1: << endl;
cin>> t2;
i = 0;
i = i + j;


j此时尚未初始化

这些已经在课程的私人部分初始化。

其次,如果我给它们是函数开头的一个值,怎么可以增加工作?

ken


" John哈里森" <乔************* @ hotmail.com>在消息中写道

news:2h ************ @ uni-berlin.de ...
Ken <乐****** @ yah00.c0m>在消息中写道
新闻:wj ************** @ news20.bellglobal.com ...

我有一个小程序,wi tha menu (只有选项1,2和6现在是
工作)
发生了一些奇怪的事情,我的数组工作输入用户

输入的所有数据,但是我是选择选项2,显示,它总是给我
4202717为数组中的元素编号1,而0,2,3 ..都没问题。
它似乎跳过数组中的数字1 。
任何人?
问题在于createTime()函数

我正在使用visual c ++
ken

#include< iostream>
#include< conio.h>
使用命名空间std;

类timeChange
{
公开:
void createTime();
void display1();

私人:
int t1,t2,i,j,a [];



]不是合法的C ++。我不知道visual c ++是什么造成的,但它



是不对的。如果你想用C ++声明一个数组,你必须说出b $ b想要它有多大。

};

内联void timeChange: :createTime()
{

if(t1> 0)



此时t1是未初始化的

{
cout<<"输入数字1: << endl;
cin>> t2;
i = 0;
i = i + j;



此时j未初始化

做{
cout<<<"在j之前: << j<< endl;
cout<<" a [i]: << a [i]<< endl;
cout<<" t2: << t2<< endl;
cout<<"在我之前: << i<< endl;
i ++;
a [i] = t2;
j = i;
cout<<" j: << j<< endl;
cout<<" a [i]: << a [i]<< endl;
cout<<" t2: << t2<< endl;
cout<<"我: <<我<< endl;
}
而(t1< 0);

}


{
cout<<"输入数字2: << endl;
cin>> t1;
i = 0;
做{
a [i] = t1;
cout<<<"在j之前: << j<< endl;
cout<<" t1: << t1<< endl;
cout<<"我: <<我<< endl;
cout<<" a [i]: << a [i]<< endl;
cout<<"在i ++之前: << i<< endl;
i ++;
j = i;

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

} //结束其他

} //结束无效函数



在给出值之前你不应该使用变量。

john



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;
}

解决方案


"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



这篇关于跳过数组中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆