c++ - 为什么循环只运行一次?

标签 c++ while-loop ofstream

<分区>

我试着做一个电话系统,我在 main{} 中使用了一个 while 循环。我不知道为什么它只运行一次,它应该运行无限次,除非我给它命令停止。

#include <iostream>
#include <string>
#include <fstream>
using namespace std;


void record(string name, int phoneNum, int count);

// main
int main() {
    cout << " Welcome to use the Phone Contact Systerm " << endl;
    string name;
    int phoneNum;
    int count = 0;
    string signToStop;
    cout << " Please enter name and phone number " << endl;
    while ( cin >> name >> phoneNum){
        cout << " If you want to start the program, enter start "     << endl;
        cout << " If you want to quit the program, enter quit " <<     endl;
        cin >> signToStop;
        if (signToStop == "start"){
            record(name, phoneNum, count);
        }
        else if ( signToStop == "quit" ){
            break;
        }
        count++;

    }
}

// record all name info into Name set and record all phone numbers     into PhoneNum set
void record(string name, int phoneNum, int count){
    string Name[] = {};
    int PhoneNum[] = {};
    Name[count] = {name};
    PhoneNum[count] = {phoneNum};

    // now start to record all the info into .txt document

    ofstream phoneFile;
    phoneFile.open("contact.txt");
    phoneFile << name << "  " << phoneNum << endl;
}

结果是:

 Welcome to use the Phone Contact Systerm 
 Please enter name and phone number 
Molly 5307659229

Process finished with exit code 0

最佳答案

也许尝试使用 ulong int 作为电话号码,它可能太长了。另外我可能会补充说我有点困惑,因为你的函数 record() 有一个没有默认参数的第三个参数。你的问题也可能在那里。由于没有默认值,您需要在使用时放入参数。

关于c++ - 为什么循环只运行一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57538652/

相关文章:

java - 来自 Java 的 OpenCV 中的逻辑操作

r - 在 while 循环中借助 if 语句让 R 跳过某些索引值

c++ - 如何恢复将数据附加到特定位置的文件? (std::ostream, streampos, tellp/seekp)

C++ 对 const 引用参数的依赖没有改变

c++ - 从 MySQL 获取 UTF-8 数据到 Linux C++ 应用程序

c++ - 共享库和静态库的用法区别

C++如何加减tellp(),tellg()返回

Python3 - 而 ids > 停止 : TypeError: unorderable types: str()> int()

Javascript 将 2 个字段与 PHP while 循环内的第三个字段中的答案相乘

c++ - 打开一个 fstream 进行读/写(二进制)