C ++函数不起作用 [英] C++ function not working

查看:72
本文介绍了C ++函数不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们我正在构建一个c ++项目,它基本上是一个银行系统

我的代码的两个功能都不起作用,我不知道为什么!!

函数被撤销并显示,

这里的代码是

  #include   <   iostream  >  
使用 命名空间标准;

struct 地址{
string stName;
int houseNum;
};
struct 帐户{
字符串名称;
int accnumber;
double 余额;
地址地址;
};

void menu(); // 程序界面
void 撤销(帐户帐户[], int acnum, double 金额);
void Create_account( int & i,Account accounts [],Address addreses []) ;
void display(账户账户[],地址addreses [], int acnum);
double 存款(double& balance, double 金额);


int main(){

while true ){

system( color 0a);
menu();

}


}
// 主菜单,程序开始
void menu(){
// 数组索引
int i = 0 ;
int choice;
帐户[[span class =code-digit> 10 ];
地址地址[ 10 ];
int acnum;
double 金额;
cout<< ENDL;
system( cls);
cout<< \t\t\t ---输入你的选择---< < ENDL;
cout<< ENDL;
cout<< \t\t\t 1 - 创建帐户\ n;
cout<< ENDL;
cout<< \t\t\t 2 - 存款<< endl;
cout<< ENDL;
cout<< \t\t\t 3 - 提取资金<< endl;
cout<< ENDL;
cout<< \t\t\t 4 - 显示<< ENDL;
cout<< ENDL;
cout<< \t\t\t 5 - 退出<< ENDL;
cout<< ENDL;
cout<< \t\t\t选项是:;
cin>>选择;
system( cls);
switch (选择){
case 1
Create_account(i,accounts,addreses);
break ;

case 2
cout<< \t\t输入您要存入的金额:;
break ;
case 3
cout<< \t \ t请输入acc编号:;
cin>> acnum;
cout<< \t\t输入金额:;
cin>>量;
提款(账户,公积金,金额);
break ;
case 4
cout<< \t \ t请输入acc编号:;
cin>> acnum;
显示(account,addreses,acnum);
break ;

break ;
case 5
cout<< 再见!!!;
退出( 0 );
break ;
}
}


// function to创建一个新帐户
void Create_account( int & i,帐户帐户[],地址addreses []){
system( cls);
cout<< \t\t\t ---创建新帐户---< < ENDL;
cout<< \t\t名称:;
cin>>帐户[I]。名称;
cout<< \t\t帐号:;
cin>>帐户[I] .accnumber;
cout<< \t\t余额:;
cin>>帐户[I] .balance;
cout<< \t\t\t ---地址信息---<< ; ENDL;
cout<< \t\t Streat Name:;
cin>> addreses [I] .stName;
cout<< \t\t门牌号:;
cin>> addreses [I] .houseNum;
i ++;
}
void withdraw(账户账户[], int acnum, double amount){
for int i = 0 ; i< = 10 ; i ++){
if (accounts [i] .accnumber == acnum){
accounts [i] .balance - = amount;
cout<< \t \ tDone。;
break ;
}
else if (i == 10 ){
cout<< \t\tAccount Not Found !!!<< ENDL;
}
}}
double 存款( double & balance , double 金额){

}

void 显示(帐户帐户[],地址地址[], int acnum){
int i = 0 ; i< = 10 ; i ++){
if (accounts [i] .accnumber == acnum){
cout<< \t\tName:<<帐户[I]。名称;
system( PAUSE);

}
}}





我的尝试:



每一件东西它都在杀我):

解决方案

当你不明白为什么你的代码失败,是时候学习调试器了。

不工作不提供信息,请详细说明功能不起作用。

使用调试器,检查变量并检查它们是否包含应有的内容。



   int  i =  0 ; i< =  10 ; i ++){



数组是10个元素,因此从1到9,循环必须以 i< 10



你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候你已经接近了一个错误。


hey guys i'm building a c++ project it's basicly a bank System
two function of my code are not working ,, i don't know why!!
functions are withdraw and display,,
here's the code

#include<iostream>
using namespace std;

struct Address{
	string stName;
	int houseNum;
};
struct Account{
	string name;
	int accnumber;
	double balance;
	Address addr;
};

void menu(); // program interface
void withdraw(Account accounts[] , int acnum , double amount);
void Create_account(int &i,Account accounts[] , Address addreses[]); 
void display(Account accounts[] , Address addreses[] , int acnum);
double deposit(double&balance , double amount);


int main(){
	
	while (true){
		
		system("color 0a");
		menu();
	
	}
	
	
}
// main Menu , program start
void menu(){
	// index of array
		int i = 0 ;
		int choice;
		Account accounts[10] ;
		Address addreses[10];
		int acnum;
		double amount;
		cout << endl;
		system("cls");
		cout << "\t\t\t --- Enter your choise --- " << endl;
		cout << endl;
		cout << "\t\t\t 1 - Create Account\n";
		cout << endl;
		cout <<"\t\t\t 2 - Deposit Money" <<endl;
		cout << endl;
		cout << "\t\t\t 3 - Withdraw Money" <<endl;
		cout << endl;
		cout << "\t\t\t 4 - Display" << endl;
		cout << endl;
		cout << "\t\t\t 5 - Exit" << endl;
		cout << endl;
		cout << "\t\t\t choice is :  ";
		cin >> choice;
		system("cls");
		switch (choice){
			case 1:
				Create_account(i, accounts , addreses);
				break;
		
			case 2:
				cout << "\t\tEnter the amount you Want to deposit :" ;
				break;
			case 3 :
				cout << "\t\tPlease enter the acc number: " ;
				cin >> acnum;
				cout << "\t\tEnter amount to withdraw : ";
				cin >> amount;
				withdraw(accounts , acnum , amount);
				break;
			case 4:
				cout << "\t\tPlease enter the acc number: " ;
				cin >> acnum;
				display(accounts , addreses , acnum);
				break;
				
				break;
			case 5:
				cout << "Good bye !!!";
				exit(0);
				break;
		}
		}
		
		
// function to Create a new account
void Create_account(int &i , Account accounts[] , Address addreses[]){
	system("cls");	
	cout << "\t\t\t --- Create New Account --- " << endl;
	cout << "\t\t Name: ";
	cin >> accounts[i].name;
	cout << "\t\t Account Number: ";
	cin >> accounts[i].accnumber;
	cout << "\t\t Balance: ";
	cin >> accounts[i].balance;
	cout << "\t\t\t --- Address Information --- " << endl;
	cout << "\t\t Streat Name: ";
	cin >> addreses[i].stName;
	cout << "\t\t House Number : ";
	cin >> addreses[i].houseNum;
	i++;
	}
void withdraw(Account accounts[] , int acnum , double amount){
	for (int i = 0; i <= 10 ;i++){
	if (accounts[i].accnumber == acnum){
		accounts[i].balance -= amount;
		cout << "\t\tDone.";
		break;
	}
	else if (i == 10 ){
		cout << "\t\tAccount Not Found !!!" << endl;
	}
}}
double deposit(double &balance , double amount){
	
}

void display(Account accounts[] , Address addreses[] , int acnum){
	for (int i = 0; i <= 10 ;i++){
	if (accounts[i].accnumber == acnum){
		cout << "\t\tName : " << accounts[i].name;
		system("PAUSE");
	
	}
}}



What I have tried:

every thing and it's killing me ):

解决方案

When you don't understand why your code fail, it is time to learn the debugger.
"Not working" is not informative, please give details in what the functions don't work.
With the debugger, inspect variables and check that they contain what they should.

for (int i = 0; i <= 10 ;i++){


the array is 10 elements, so from 1 to 9 the loop must end with i < 10

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.


这篇关于C ++函数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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