[英]Strange character in C++ program output
I am creating an ATM program in C++ for school and I'm using this project as an opportunity to begin learning the language. 我正在用C ++创建一个用于学校的ATM程序,并将这个项目作为开始学习该语言的机会。 I am trying to output bank accounts to a text file in this format:
我正在尝试以这种格式将银行帐户输出到文本文件:
First Last
CardNumber
PinNumber
Balance
Originally it was outputting fine and then I developed a bunch of new methods etc. I didn't change any of the original code that has to do with outputting the bank account but now it's outputting strangely. 最初它可以很好地输出,然后我开发了许多新方法,等等。我没有更改任何与输出银行帐户有关的原始代码,但是现在它的输出很奇怪。
My output ends up being: 我的输出最终是:
First Last
random letter or symbol
PinNumber
blank
Here is my code for creating a new bank account: 这是我创建新银行帐户的代码:
AccountHandler.h AccountHandler.h
#include <iostream>
#include <string>
using namespace std;
struct bankAccount //Create a new bank account type
{
string name;
string cardNum;
string balance;
string pin;
};
class AccountHandler
{
public:
AccountHandler();
~AccountHandler();
void withdraw(struct bankAccount acc, string amount);
void deposit(struct bankAccount acc);
int checkBalance(struct bankAccount acc);
void createAccount();
};
AccountHandler.cpp AccountHandler.cpp
void AccountHandler::createAccount() //creates a new bank account and stores in accounts.txt
{
ofstream accounts;
struct bankAccount newAcc;
string first, last;
string tempPin1, tempPin2;
if (!accounts.is_open())
{
accounts.open("accounts.txt", ofstream::app);
}
std::cout << "Thank you for choosing to bank with ATM406!\n\n";
std::cout << "Please enter the name for the account: ";
std::cin >> first >> last;
newAcc.name = first + " " + last;
while (true)
{
std::cout << "\nPlease enter a 4-digit pin for security: ";
std::cin >> tempPin1;
std::cout << "\nPlease re-enter your 4-digit pin for validation: ";
std::cin >> tempPin2;
if (tempPin1 == tempPin2) //PINS MATCH
{
newAcc.pin = tempPin1;
break;
}
else //PINS DO NOT MATCH
{
std::cout << "The pins did not match!" << std::endl;
}
}
//GENERATE A RANDOM 4-DIGIT NUMBER FOR CARDNUM
srand(time(NULL));
newAcc.cardNum = rand() % 9000 + 1000;
//STORE ACCOUNT IN FORMAT: NAME\nCARDNUM\nPIN\nBALANCE
accounts << newAcc.name << "\n" << newAcc.cardNum << "\n"
<< newAcc.pin << "\n" << newAcc.balance << "\n";
accounts.close();
std::cout << "\nAccount created with name: " << newAcc.name << " pin: " << newAcc.pin
<< ". Card number: " << newAcc.cardNum << "\n\n\n";
}
Thank you! 谢谢!
cardNum
is a string, but you assign an integer to it. cardNum
是一个字符串,但是您为其分配了一个整数。 That converts the integer to a char
(truncating it to a much smaller value) and stores it in the string. 它将整数转换为
char
(将其截断为更小的值)并将其存储在字符串中。
balance
is blank because it's an empty string, you never give the string a value. balance
为空,因为它是一个空字符串,所以您永远不要给字符串赋值。
NB the call to is_open()
in createAccount
is pointless, the fstream can't be open, because you just default-constructed it. 注意,对
createAccount
is_open()
的调用是没有意义的,无法打开fstream,因为您只是默认构造了它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.