如何在C++中的循环外使用For循环中的变量
how do I use the variable from a For loop outside the loop in C++?
所以我的编程技能有点生疏,除了过去上过的大学课程外,没有任何现实世界的经验。我正在为一个班编写一个程序,但遇到了一个障碍;我不知道如何在循环外的For
循环中使用变量的值。这是我引用的代码:
#include "iostream"
using namespace std;
int want, have, need;
int counter = 0;
char response, cont;
int diff(int want, int have);
int main(){
cout << "Welcome!n";
cout << "This program will help you reach your money saving goals!n";
cout << "Would you like to use this program? (y/n)n";
cin >> response;
while (response == 'y'){
cout << "Please enter all amounts in whole dollars only!n";
cout << "Please enter the amount of money you would like to have saved: $";
cin >> want;
cout << "nPlease enter the amount of money you currently have saved: $";
cin >> have;
if (have >= want){
cout << "You have already reached or exceeded your goal, this program will not be able to help you!n";
system("Pause");
return 0;
}
cout << "nYou need to save $" << diff(want, have) << " more money to reach your goal!n";
cout << "Would you like me to help you with a savings plan?";
cin >> cont;
while (cont == 'y'){
int menu;
cout << "Please select from the following options: n";
cout << "1 - Daily Saving Plann";
cout << "2 - Weekly Saving Plann";
cout << "3 - Monthly Saving Plann";
cout << "Enter the number associated with your choice: n";
cin >> menu;
switch (menu){
case 1: {
int daily;
cout << "You have chosen the Daily Savings Plann";
cout << "How much money can you save every day? $";
cin >> daily;
for (int x = daily; x < need; x++){
daily = daily + daily;
counter++;
}
cout << "nIt will take you " << counter << " days to reach your goal!n";
break;
}
case 2: {
int weekly;
cout << "You have chosen the Weekly Savings Plann";
cout << "How much money can you save every week? $";
cin >> weekly;
for (int x = weekly; x < need; x++){
counter++;
}
cout << "nIt will take you " << counter << " weeks to meet your goal!n";
break;
}
case 3: {
int monthly;
cout << "You have chosen the Monthly Savings Plann";
cout << "How much money can you save every month? $";
cin >> monthly;
for (int x = monthly; x < need; x++){
monthly = monthly + monthly;
counter++;
}
cout << "nIt will take you " << counter << " months to reach your goal!n";
break;
}
default: cout << "You made an invalid selection";
cout << "Would you like to look at a different saving plan? (y/n)n";
cin >> cont;
}
}
}
}
int diff(int want, int have){
return want - have;
}
所以,当我运行程序时,一切都正常,但计数器的值在最后的cout
语句中总是显示为"0"。
我理解它为什么这么做,我想。。这是因为循环外的"int counter=0"声明,所以我假设它在循环退出后会返回到那个值。
如果我不初始化计数器变量,我会得到一个错误,如果我在循环中声明该值,我会在尝试在cout语句中使用它时得到一个误差,就像上面所说的那样。
我甚至不确定我的for循环结构是否正确。。。基本上,我希望它添加到自己的每周变量,直到x = need
的总和。我还想捕捉它需要多少次迭代,然后将其输出为周数。希望这一切都有意义;感谢您的任何帮助。
你想做的事情似乎可以用ceil(double(need/weekly))
来完成,而这四舍五入的需求除以每周。
循环外的声明不会影响完成for
循环后cout
的值。
至于你的问题,看起来你从未初始化过need
,所以你的for循环从未进行过迭代,因为undefined不小于或等于0。
程序的C++11中的草图。
char prompt( std::initializer_list<const char*> message, std::initializer_list<const char*> question, std::initializer_list<char> options ) {
for( auto msg:message )
std::cout << msg;
while(true) {
char response;
for( auto q:question)
std::cout << q;
std::cin >> response;
for (char option:options) {
if (response == option)
return response;
}
}
}
int prompt( std::initializer_list<const char*> message, std::initializer_list<const char*> question, int min = 0, int max = std::numeric_limits<int>::max() ) {
for( auto msg:message )
std::cout << msg;
while(true) {
int response;
for( auto q:question)
std::cout << q;
std::cin >> response;
if (response >= min && response <= max)
return response;
}
}
}
void saving( const char* plan, const char* unit, const char* units, int target ) {
int daily = prompt(
{"You have chosen the ", plan, "n"},
{"How much money can you save every ", unit, "? $"},
1 // min saving of 1$ per unit time to prevent infinite loops
);
std::cout << "n";
int counter = 0;
int amount_saved = 0;
while (amount_saved < target) {
++counter;
amount_saved += daily;
}
if (counter != 1)
std::cout << "It will take you " << counter << " " << units << " to reach your goaln";
else
std::cout << "It will take you " << counter << " " << unit << " to reach your goaln";
}
int main() {
while(
prompt(
{"Welcome!nThis program will help you reach your money saving goals!n"},
{"Would you like to use this program? (y/n)n"},
{'y', 'n'}
) == 'y'
)
{
int want = prompt( {"Please enter all amounts in whole dollars only!n"},
{"Please enter the amount of money you would like to have saved?"} );
int have = prompt( {"n"}, {"Please enter the amount of money you currently have saved?n"} );
std::cout << "n";
if (have >= want) {
std::cout << "You win!n";
system("Pause"); // ick
return 0;
}
std::cout << "You need to save $" << (have-want) << " more money to reach your goal!n";
while( 'y' == prompt(
{},
{"Would you like me to help you with a savings plan? (y/n)"},
{ 'y', 'n' }
)) {
char menu = prompt(
{
"Please select from the following options: n",
"1 - Daily Saving Plann",
"2 - Weekly Saving Plann",
"3 - Monthly Saving Plann"
},
{"Enter the number associated with your choice: n"},
{ '1', '2', '3' }
);
switch (menu) {
case '1': {
saving( "Daily Saving Plan", "day", "days", have-want);
} break;
case '2: {
saving( "Weekly Saving Plan", "week", "weeks", have-want);
} break;
case '3': {
saving( "Monthly Saving Plan", "month", "months", have-want);
} break;
}
}
}
}
相关文章:
- 如何在C++中从两个单独的for循环中添加两个数组
- 为什么我的for循环不能正确获取argv
- 在基于范围的for循环中使用结构化绑定声明
- 通过for循环使用用户输入填充列表
- 使用for循环检查数组中的重复项
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 正在使用for循环创建QScatterSerie
- Python中的for循环与C++有何不同
- 在更改for循环的第三部分后,未使用for循环结果
- 在 for 循环中查找问题时遇到困难
- 嵌套for循环C++的问题(初学者)
- 如何用for循环在c++中生成单词三角形
- 如何在for循环中包含两个索引值的测试条件
- 带有多个独立参数的C++For循环
- C++ Python 循环"for i, num in enumerate(list):"版本
- C 多循环 for () 基础知识
- 没有条件值的 FOR 循环"for (int i = 1; ; i++)"无法正常工作
- 为用户提供循环for循环的选项
- 打破循环for循环