- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我如何解决这个变量初始化问题?如果我只能弄清楚如何只初始化它们一次...
* Main.cpp : main project file.
/************************** Begin Header **************************/
#include "stdafx.h" //Required by Visual C ++
#include <string> //Required to use strings
#include <iostream> //Required for a bunch of important things
#include <iomanip> //Required for manipulation of text and numbers
using namespace System; // Automatically uses System namespace
using namespace std; // Automatically uses std namespace
#pragma hdrstop // Stops header here
/*************************** End Header ***************************/
//* Begin Function Headers *//
void inputData(); // This will be used to organize class member calls when setting and getting new data.
int getData(); // Will get user data, input in character string, convert to an integer and then perform data validation.
void createReport(int place, int number, string type); // Will organize commands to create the report and display it on the screen.
//* End Function Headers *//
class JarsSold // Begin Class -- JarsSold
{
/* Begin Initialization & Creation of important resources */
private:
static const int MaxArray = 5; // Value for the size of array JARS_SOLD
int JARS_SOLD[MaxArray]; // Creation of array with size of MaxArray
/* End Initialization & Creation of important resources */
public: // Makes underlining elements Public instead of the default Private
JarsSold() // Begin Constructor
{ // Put something in num array
JARS_SOLD[0] = 0; // [1]
JARS_SOLD[1] = 0; // [2]
JARS_SOLD[2] = 0; // [3]
JARS_SOLD[3] = 0; // [4]
JARS_SOLD[4] = 0; // [5]
} // End Constructor
~JarsSold(){}; // Destructor
/* Put all members for JarsSold class below here */
void setNumber(int num, int value) // Set the number of jars sold with number placement in array and value to replace it with
{
JARS_SOLD[num] = value; // Stores value into JARS_SOLD at whatever num is at the time
}; // End setNumber class member
int getNumber(int num) // Get the current number held for jars sold with number placement in array
{
return JARS_SOLD[num]; // Returns whatever is in JARS_SOLD depending on what num is at the time
} // End getNumber class member
/* Put all members for JarsSold class above here */
}; // End Class -- JarsSold
class SalsaTypes // Begin Class -- SalsaTypes
{
/* Begin Initialization & Creation of important resources */
private:
static const int MaxArray = 5; // Value for the size of array JARS_SOLD
string SALSA_TYPES[MaxArray]; // Creation of array with size of MaxArray
/* End Initialization & Creation of important resources */
public: // Makes underlining elements public instead of the default Private
SalsaTypes() // Begin Constructor
{ // Add default strings to str array
SALSA_TYPES[0] = "Mild"; // [1] Stores Mild into SALSA_TYPES at 0 spot
SALSA_TYPES[1] = "Medium"; // [2] Stores Medium into SALSA_TYPES at 1 spot
SALSA_TYPES[2] = "Sweet"; // [3] Stores Sweet into SALSA_TYPES at 2 spot
SALSA_TYPES[3] = "Hot"; // [4] Stores Hot into SALSA_TYPES at 3 spot
SALSA_TYPES[4] = "Zesty"; // [5] Stores Zesty into SALSA_TYPES at 4 spot
} // End Constructor
~SalsaTypes(){}; // Destructor
/* Put all members for SalsaTypes class below here */
void setType(int num, string type) // Set salsa type with number placement in array and string value to replace with
{
SALSA_TYPES[num] = type; // Stores the string type into SALSA_TYPES at whatever num is at the time
}; // End setType class member
string getType(int num) // Get the Salsa Type with number placement in array
{
return SALSA_TYPES[num]; // Returns SALSA_TYPES depending on what is in num at the time
}; // End getType class member
/* Put all members for SalsaTypes class above here */
};// End Class -- SalsaTypes
void main( void ) // Begin Main Program
{
cout << fixed << setprecision(1) << setw(2); // Do a little customization with IoManip, might as well, we just might need it
// Main Program Contents Begin Here //
// Opening Credits for Program
cout << "Welcome to the /Professional Edition\\ of the Chip and Salsa Sale Tool EXPRESS." << endl;
cout << "This handy-dandy tool will make a chip and salsa manufacturer's job much easier!" << endl;
cout << endl << endl << endl;
cout << "Press any key to begin inputing the number of jars sold for these salsa flavors: " << endl << endl;
cout << "-Mild" << endl << "-Medium" << endl<< "-Sweet" << endl << "-Hot" << endl << "-Zesty" << endl << endl << endl;
system("pause"); // Pause here. After this begin data input
cout << endl << endl << endl;
inputData(); // Will deal with data input, validation, and reports
// Main Program Contents End Here //
} //End Main Program
// All Content for Functions Begin Here //
void inputData() // Begin inputData Function
{
// Begin Create Class Obects //
SalsaTypes salsa;
JarsSold jars;
// End Create Class Objects //
// Variable Creation Begin //
// Variable Creation End //
// All Content for Functions Begin Here //
for (int i = 0 ; i < 5 ; i++) // Start For Loop
{
cout << "Input how many Jars were sold for \"" << salsa.getType(i) << "\"" << ": "; // Displays which Salsa we are reffering to
jars.setNumber(i,getData()); // Will use getData() to determine what value to send to the JarsSold class.
createReport(i,jars.getNumber(i),salsa.getType(i)); // Send these numbers to another function so it can make a report later
cout << endl << endl; // Using this as a spacer
}
// All Content for Functions End Here //
}; // End inputData Function
int getData() // Begin getData Function
{
// Variable Creation Begin //
char charData[40]; // Will be used to store user data entry
int numTest; // Will be used for Data Validation methods
// Variable Creation End //
// Main Contents of Function Begin Here //
retry: // Locator for goto command
cin >> charData; // Ask user for input. Will store in character string then convert to an integer for data validation using 'Atoi'
numTest = atoi ( charData ); // Convert charData to integer and store in numTest
if (numTest < 0) { numTest = 0; cout << endl << endl << "You can't enter negative numbers! Try Again." << endl << endl << "Re-enter number: "; goto retry;} // Filter out negative numbers
// Main Contents of Function End Here //
return numTest; // If it makes it this far, it passed all the tests. Send this value back.
}; // End getData Function
void createReport(int place, int number, string type) // Begin createReport Function
{
// Variable Creation Begin //
int const MAX = 5; // Creat array size variable
int lowest; // The integer it will use to store the place of the lowest jar sales in the array
int highest; // The integer it will use to store the place of the highest jar sales in the array
int total; // The integer it will use to store total sales
int numArray[MAX]; // Create array to store jar sales (integers)
string typeArray[MAX]; // Create array to store types of salsa (strings)
// Variable Creation End //
// Main Contents of Function Begin Here //
numArray[place] = number; // Store number into new array
typeArray[place] = type; // Store type into new array
if (place = 4) // If the array is full it means we are done getting data and it is time to make the report.
{ // Begin making report, If statement start
for ( int i = 0 ; i < 5 ; i++ ) // Using a for-loop to find the highest and lowest value in the array
{ // For Loop to find high and low values BEGIN
if ( lowest < numArray[i]) // Keep setting lowest to the lowest value until it finds the lowest one
{ // Start If
lowest = numArray[i]; // Lowest equals whatever is in numArray at i spot
} // End If
if ( highest > numArray[i]) // Keep setting highest to the highest value until it finds the highest one
{ // Start If
highest = numArray[i]; // Highest equals whatever is in numArray at i spot
} // End If
total += numArray[i]; // Will continually add numArray at i spot until it has the total sales
} // For Loop to find high and low values END
// Main Contents of Function End Here //
} // END creatReport Function
// All Content for Functions Ends Here //
嗯,我的问题是……我需要将我的数据从一个功能转移到另一个功能。我以为我可以弄清楚如何创建全局类对象,但我做不到。所以我想我可以解决这个问题,只需将参数传递给另一个函数,然后将它们恢复到它自己的数组中,然后继续这样做,直到我完全复制了所有数字数组和字符串数组。好吧......是的,除了 createReport() 中的这一部分:
// Variable Creation Begin //
int const MAX = 5; // Create array size variable
int lowest; // The integer it will use to store the place of the lowest jar sales in the array
int highest; // The integer it will use to store the place of the highest jar sales in the array
int total; // The integer it will use to store total sales
int numArray[MAX]; // Create array to store jar sales (integers)
string typeArray[MAX]; // Create array to store types of salsa (strings)
// Variable Creation End //
发生的事情是,我想我太累了以至于错过了它,每次我调用该函数时它都会再次重新初始化那些相同的变量。我会将变量放入另一个变量,然后它将重新初始化为默认值。
我尝试使用一个计数器变量,它在初始化后计数为 1,然后在计数为 1 后它不会再次初始化。不,那没有用,因为变量没有在 if 语句的范围之外初始化。然后我尝试了一个 GOTO 语句,它会在它发生一次后跳过初始化。不,第一个初始化阶段出了点问题,没有编译。
我需要弄清楚我该怎么做
我还不太擅长编程。但我向你保证,我已经为这件作品工作了好几个小时,好几个小时,好几个小时,而且我整晚都在熬夜,只是不断地试错。我真的为自己感到骄傲,因为这段代码对我来说非常先进。我查看了 Google 上的每个教程,但我很不走运——你们是我最后的希望!!再次抱歉,伙计们。我知道这是一个愚蠢的问题...
最后一个简短的问题。如何创建全局类对象?例如
MyClass
{
MyClass(){};
~MyClass(){};
}
MyClass MyClassObject;
如何在我的整个程序中使用 MyClassObject?
我可以使用它的唯一方法是每次使用我使用的每个函数时都创建一个新对象。这意味着我丢失了我类(class)中存储的所有数据?
我读到过拥有全局对象并不是一个好主意。我很想不必使用它们,但我不知道我能真正理解的任何替代方案。
非常感谢任何其他批评或提示。我喜欢这个东西,我只是没有很多人可以问问题。
最佳答案
你做得很好!简单的答案是在变量前写上 static:
static int const MAX = 5; // Creat array size variable
static int lowest; // The integer it will use to store the place of the lowest jar sales in the array
static int highest; // The integer it will use to store the place of the highest jar sales in the array
static int total; // The integer it will use to store total sales
static int numArray[MAX]; // Create array to store jar sales (integers)
static string typeArray[MAX]; // Create array to store types of salsa (strings
不过我想我可以给你更好的建议,我会花更长的时间查看你的代码。至于全局变量,你用 MyClassObject 在那里写的东西就可以了。你会像这样使用它:
MyClass {
public:
MyClass(){};
~MyClass(){};
int variable;
};
MyClass MyClassObject;
// in a method you'd do this
void whateverMethod() {
MyClassObject.variable = 5;
std::cout << MyClassObject.variable << std::endl;
}
那种东西。有一些样式问题可以解决,但老实说,我只是说先让它工作,然后我们再讨论这些问题。
关于c++ - 每次调用 C++ 函数时,如何防止变量被重新初始化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/495540/
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: How to nest OR statements in JavaScript? 有没有办法做到这一点:
在 JavaScript 中有没有办法让一个变量总是等于一个变量?喜欢var1 = var2但是当var2更新,也是var1 . 例子 var var1 = document.getElementBy
我正在努力理解这代表什么 var1 = var2 == var3 我的猜测是这等同于: if (var2 == var3): var1 = var2 最佳答案 赋值 var1 = var2
这个问题已经有答案了: What does the PHP error message "Notice: Use of undefined constant" mean? (2 个回答) 已关闭 8
我在临时表中有几条记录,我想从每条记录中获取一个值并将其添加到一个变量中,例如 color | caption -------------------------------- re
如何将字符串转为变量(字符串变量--> $variable)? 或者用逗号分隔的变量列表然后转换为实际变量。 我有 2 个文件: 列名文件 行文件 我需要根据字符串匹配行文件中的整行,并根据列名文件命
我有一个我无法解决的基本 php 问题,我也想了解为什么! $upperValueCB = 10; $passNodeMatrixSource = 'CB'; $topValue= '$uppe
这可能吗? php $variable = $variable1 || $variable2? 如果 $variable1 为空则使用 $variable2 是否存在类似的东西? 最佳答案 PHP 5
在 Perl 5.20 中,for 循环似乎能够修改模块作用域的变量,但不能修改父作用域中的词法变量。 #!/usr/bin/env perl use strict; use warnings; ou
为什么这不起作用: var variable; variable = variable.concat(variable2); $('#lunk').append(variable) 我无法弄清楚这一点
根据我的理解,在32位机器上,指针的sizeof是32位(4字节),而在64位机器上,它是8字节。无论它们指向什么数据类型,它们都有固定的大小。我的计算机在 64 位上运行,但是当我打印包含 * 的大
例如: int a = 10; a += 1.5; 这运行得很完美,但是 a = a+1.5; 此作业表示类型不匹配:无法从 double 转换为 int。所以我的问题是:+= 运算符 和= 运算符
您好,我写了这个 MySQL 存储过程,但我一直收到这个语法错误 #1064 - You have an error in your SQL syntax; check the manual that
我试图在我的场景中显示特定的奖牌,这取决于你的高分是基于关卡的目标。 // Get Medal Colour if levelHighscore goalScore { sc
我必须维护相当古老的 Visual C++ 源代码的大型代码库。我发现代码如下: bIsOk = !!m_ptr->isOpen(some Parameters) bIsOk的数据类型是bool,is
我有一个从 MySQL 数据库中提取的动态产品列表。在 list 上有一个立即联系 按钮,我正在使用一个 jquery Modal 脚本,它会弹出一个表单。 我的问题是尝试将产品信息变量传递给该弹出窗
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: What is the difference between (type)value and type(va
jQuery Core Style Guidelines建议两种不同的方法来检查变量是否已定义。 全局变量:typeof variable === "undefined" 局部变量:variable
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: “Variable” Variables in Javascript? 我想肯定有一种方法可以在 JavaScrip
在语句中使用多重赋值有什么优点或缺点吗?在简单的例子中 var1 = var2 = true; 赋值是从右到左的(我相信 C# 中的所有赋值都是如此,而且可能是 Java,尽管我没有检查后者)。但是,
我是一名优秀的程序员,十分优秀!