Visual C ++ - 运行时检查失败#3 - 未初始化变量 [英] Visual C++ - Runtime Check Failure #3 - Variable is not initiliazed

查看:420
本文介绍了Visual C ++ - 运行时检查失败#3 - 未初始化变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Visual C ++ 2010 Express Edition来编译和运行在C ++编程语言中编写的.exe文件。我试图创建一个基于循环的逻辑,使用C ++来询问用户他选择输入多少条目,并提出问题限于没有。的条目。例如,我想输出,你想要输入多少字符:说用户给出的答案为'3',存储在int变量'entries'。然后我想继续问这个问题3次,然后停止,并继续下一行代码。我希望你明白,这里是一块代码来演示我在做什么:

I use Visual C++ 2010 Express Edition to compile and run the .exe files I write in the C++ programming language. I am trying to create a loop-based logic using C++ to ask the user how many entries he chooses to enter, and ask questions limited to that no. of entries. For example I want to output, "How many characters do you wish to enter?: " Say the user gives the answer as '3' which is stored in the int variable 'entries'. I then want to keep asking the question 3 times before it stops and continues with the next line of code. I hope you understand, here is a block of code to demonstrate what I am doing:

#include <iostream>
#include <string>
using namespace std;

int main()
{
   cout << "How many values do you need to enter?: ";
   int entries;
   cin >> entries;
   int offset, number;
   string valueName[50];
   float valueValue[50];
   for (offset = 0; offset < entries; offset++)
   {
      cout << "Enter " << number << " Value Name: ";
      cin >> valueName[offset];
      cout << "Enter " << valueName[offset] << "\'s value: ";
      cin >> valueValue[offset];
      for (number = 1; number <= entries; number++)
      {
      }
   }
   char response;
   cin >> response;
   return 0;
}

奇怪的是,当我运行这个简单的程序时,插入到valueName []数组的第0个元素。它只是暂停程序的执行,弹出一个对话框,说运行时检查失败#3 - 变量'数'正在使用,而不被初始化!关于这个程序的另一个问题是,在一段时间,当我运行这个程序时,运行时检查失败#3框从未出现,当它没有出现,数值错误,首先从1开始,然后为下一个循环跳到6,然后重复6再次下一个循环!
请帮助我!我已经检查了在线搜索这个问题无处不在,但它只是不适用于我的类型的问题!
是因为变量超出范围吗?
但是它们在for循环之外被声明了?

请帮助我!

Strangely when I run this simple program, it fails when I enter the value's name to be inserted into the 0th element of the valueName[] array. It just pauses the execution of the program and a dialog box pops up saying "Runtime Check Failure #3 - Variable 'number' is being used without being initialized!" Another problem regarding this program is that, for quite some time, when I ran this program this "Runtime Check Failure #3" box never appeared, and when it didn't, the number value went wrong, and first started with 1, and then for the next loop jumped to 6, and then repeated 6 again for the next loop!
Please help me! I've checked online scouring this problem everywhere, but it just doesn't apply to my type of problem!
Is it because the variables are out of scope?
But they're declared outside the for loops right?

So please help me!

推荐答案

运行时告诉你真相,已将数字声明为int,但尚未赋予其值。

The runtime is telling you the truth, the following line comes after you have declared number as an int but have not given it a value.

 cout << "Enter " << number << " Value Name: ";

在您的代码中,您声明以下内容,在C ++中这意味着给我2个int,但值不是例如

In your code you declare the following, in C++ this means give me 2 ints but the values are not defined yet, e.g.

int offset, number;

将其更改为类似的东西。

Change it to something like this ..

int offset = 0;
int number = 0;

这篇关于Visual C ++ - 运行时检查失败#3 - 未初始化变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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