【发布时间】:2026-01-03 13:30:01
【问题描述】:
我知道我需要尽可能避免使用全局变量,但不知何故我需要在其他函数中使用某些变量。 这个程序是一个食谱程序。添加、删除和查看食谱。一切正常。也许不是最好的代码,但它可以工作。任务的第二部分是使它: 用户输入他需要为食谱服务的人数。该程序必须更改配方数量以满足他的需要。
我的计划是创建一个名为“乘数”的变量,该变量乘以每个数量,但我不知道是否有更好的方法。不幸的是,由于我需要的变量在“addRecipe”中,我无法访问它们。我需要“int people”和“int quan[]”,所以我可以将他想要的份数除以食谱的人数。这将给我一个乘数,然后我用它来乘以数组中的每个 NUMERICAL 值。然后在不更改配方的情况下以不同方式输出数组。 这可能不是最好的计划,我已经使用参数尝试了几次,但我似乎无法让它发挥作用。代码如下,我的尝试也是如此。
// Recipe.cpp : Defines the entry point for the console application.
//
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
// Practice.cpp : Defines the entry point for the console application.
//
// 2a
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
using namespace std;
void deleteRecipe(){
DIR *dp;
struct dirent *ep;
dp = opendir ("C:\\Users\\The\\Documents\\Visual Studio 2010\\Projects\\Recipe\\Recipe");
if (dp != NULL)
{
while (ep = readdir (dp))
puts (ep->d_name);
(void) closedir (dp);
}
else
perror ("Couldn't open the directory");
cout<< "Please input the file you would like to delete from the list above: "<<endl;
string dName;
cin>>dName;
remove(dName.c_str());
cout<<"The recipe has BEEN DELETED! " <<endl;
system("pause");
}
int addRecipe(){
//PART B OF PROBLEM!
int count = 1;
string title;
string item;
int quan [100];
string units;
int people;
cout<<"What is the title of your new recipe?"<<endl;
cin.ignore();
getline(cin, title);
ofstream outFile(title+".txt", ios::out);
cout<< "How many people will this recipe serve?"<<endl;
cin>>people;
cout<< title << " - Servings: " << people << " people" <<endl;
cout<<" Please type your ingredients in the format as follows. Be to sure to input a '#'upon completion."<<endl;
while (count<1000){
cout<< "Enter Item: "<<endl;
cin.ignore();
getline(cin, item,'#');
cout<< "Enter Quantity: "<<endl;
cin>>quan[];
cout<< ("Enter units")<<endl;
getline(cin, units);
count = count++;
}
cout<< "Thank you for inputting your recipe. To view your recipe, please do nothing."<<endl;
outFile << title <<endl;
outFile<< people << " People" <<endl;
outFile<< item<<endl;
return 0;
}
int viewRecipe(){
DIR *dp;
struct dirent *ep;
dp = opendir ("C:\\Users\\The\\Documents\\Visual Studio 2010\\Projects\\Recipe\\Recipe");
if (dp != NULL)
{
while (ep = readdir (dp))
puts (ep->d_name);
(void) closedir (dp);
}
else
perror ("Couldn't open the directory");
cout<<"Please type in the name file you want to view from the list above: "<<endl;
string fileName;
string line;
cin>> fileName;
ifstream inData;
inData.open(fileName.c_str());
if (inData.is_open())
{
while ( getline (inData,line) )
{
cout << line << '\n';
}
inData.close();
int multiplier = 0;
char choice;
int newAmount;
cout<<"That was the original. If you would like to view the same recipe with a different amount of servings please press '#'."<<endl;
// PART A OF PROBLEM!
if (choice= '#'){
cout<<"How many servings would you like to adapt the recipe to?"<<endl;
cin>>newAmount;
multiplier = newAmount/ addRecipe(0, );
}
}
return 0;
}
int menu(){
int input = 0;
cout<< "1: View Recipe" <<endl;
cout<< "2: Add Recipe" <<endl;
cout<< "3: Delete Recipe" <<endl;
cout<< "Please select an option"<<endl;
cin>>input;
if (input == 1){
viewRecipe();}
if (input == 2){
addRecipe();}
if (input == 3){
deleteRecipe();}
return 0;
}
int main(){
menu();
system("pause");
cin.get();
return 0;
}
我的尝试不起作用:
int addRecipe(int people, int quan[]){
int count = 1;
string title;
string item;
int quan [100];
string units;
int people;
cout<<"What is the title of your new recipe?"<<endl;
cin.ignore();
getline(cin, title);
ofstream outFile(title+".txt", ios::out);
cout<< "How many people will this recipe serve?"<<endl;
cin>>people;
cout<< title << " - Servings: " << people << " people" <<endl;
cout<<" Please type your ingredients in the format as follows. Be to sure to input a '#'upon completion."<<endl;
while (count<1000){
cout<< "Enter Item: "<<endl;
cin.ignore();
getline(cin, item,'#');
cout<< "Enter Quantity: "<<endl;
cin>>quan[];
cout<< ("Enter units")<<endl;
getline(cin, units);
count = count++;
}
cout<< "Thank you for inputting your recipe. To view your recipe, please do nothing."<<endl;
outFile << title <<endl;
outFile<< people << " People" <<endl;
outFile<< item<<endl;
return 0;
}
int viewRecipe(int multiplier){
DIR *dp;
struct dirent *ep;
dp = opendir ("C:\\Users\\The\\Documents\\Visual Studio 2010\\Projects\\Recipe\\Recipe");
if (dp != NULL)
{
while (ep = readdir (dp))
puts (ep->d_name);
(void) closedir (dp);
}
else
perror ("Couldn't open the directory");
cout<<"Please type in the name file you want to view from the list above: "<<endl;
string fileName;
string line;
cin>> fileName;
ifstream inData;
inData.open(fileName.c_str());
if (inData.is_open())
{
while ( getline (inData,line) )
{
cout << line << '\n';
}
inData.close();
int multiplier = 0;
char choice;
int newAmount;
int x = addRecipe(people, void)
cout<<"That was the original. If you would like to view the same recipe with a different amount of servings please press '#'."<<endl;
if (choice= '#'){
cout<<"How many servings would you like to adapt the recipe to?"<<endl;
cin>>newAmount;
multiplier = newAmount/ addRecipe(, );
}
【问题讨论】:
-
搜索“未声明的标识符”-该错误消息(应该逐字包含)和导致它的行是整个帖子中唯一相关的信息。
-
你有没有想过创建一个
class Recipe?然后创建一个方法viewServings(int multiplier),它将人数作为参数并返回相乘的比例? -
纳赛尔,我从来没想过。我有点初学者,但非常感谢! :) 我希望它有效:D
标签: c++ variables compiler-errors global undeclared-identifier