精英盒子 -> 程序设计 -> 运行顺序出错~~~ [打印本页]

编程土豆 2011-10-02 23:38

运行顺序出错~~~

[attachment=92]
这段代码哪里有问题,怎么跟我的预期不一样?
  1. #include  <iostream>
    #include  <string>
    //需要文件操作
    #include  <fstream>
    using  namespace  std;

    int  main()
    {
        cout  <<  "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
        cout  <<  "\t欢迎使用GnoteBook记事笔记本\t\n";
        cout  <<  "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";

        string  note,fileName;
        ofstream file;
        cout  <<  "请选择:\n";
        cout  <<  "1.新建记事本请输入1\t2.打开记事本请输入2\t\n";
        //选择模式
        cout  <<  "请选择:";
        int  mode(0);
        cin  >>  mode;

        if(mode  !=  1)
        {
            if(mode  ==  2)
            {
                cout  <<  "此功能还未完成!敬请期待!\n";
            }
            else
            {
                //如果用户输入了无效的选择,推出
                cout  <<  "错误~~~~退出鸟~~~\n";
                return  -1;
            }
        }
        else
        {
            cout  <<  "给你的便条起个名字吧(注意:要把.txt后缀带上哦!):";
            cin  >>  fileName;
            //如果用户未输入,则用默认名称
            if(fileName.empty())
            {
                cout  <<  "你没有输入文件名?没关系,那就使用默认的文件名gnote.txt\n";
                fileName  =  "gnote.txt";
            }
            else
            {
            //创建文件
            file.open(fileName.c_str(), ios::app);
            cout  <<  "请输入文本:\n";
            cout  <<  "输入完毕后,Windows用户请按ctrl+z,Linux用户和Mac用户请按ctrl+d~~~\n";
            while(getline(cin,note));
            }
        }
            cout  <<  "\n你想做什么?\n";
            cout  <<  "1.字数统计请输入1\t2.内容搜索请按2\t3.保存文件请输入3\n";
            cout  <<  "请输入选择:";
            int  mode1  =  0;
            cin  >>  mode1;
            if(mode1  ==  3)
            {
                cout  <<  endl;
                cout  <<  "文件已保存在程序所在目录!!";
                //把用户输入的内容保存到文件内
                file  <<  note;
                cout  <<  endl;
            }
            else
            {
                if(mode1  ==  2)
                {
                    cout  <<  "请问你要查找第几个字符:";
                    int  i  =  0;
                    cin  >>  i;
                    if(i  >  note.empty())
                    {
                        cout  <<  "错误,输入字符数大于文本字符书~~~~退出鸟~~~\n";
                        return  -1;
                    }
                    cout  <<  "第"  <<  i  <<  "个字符是"  <<  note[i];
                    cout  <<  endl;
                    cout  <<  "文件已保存在程序所在目录!!\n";
                    return  0;
                }
                else
                {
                    if(mode1  ==  1)
                    {
                        cout  <<  "字数(包括空格和标点)有"  <<  note.size();
                        cout  <<  endl;
                        cout  <<  "文件已保存在程序所在目录!!\n";
                        return  0;
                    }
                    else
                    {
                        cout  <<  "错误1~~~~退出鸟~~~\n";
                        return  -1;
                    }
                }
            }
    }

怎么成这样了?
[attachment=92]

编程土豆 2011-10-02 23:38
我还没选择就推出了?

jybox 2011-10-02 23:47
发现了,我觉得是你有些地方没用endl,导致有些内容被缓存起来。你看到的提示和程序的状态不同步,我是这样估计的

abreto 2011-10-03 00:23
单步调试一下

scxyscxy 2011-10-04 09:37
。。。。还@了我。。。我不会C++。。。。。。。
没去学

whtsky 2011-10-04 09:55
在这种小程序上用qt......
没必要吧…

编程土豆 2011-10-04 12:29
重写了一便,咋还是这样?
  1. #include  <iostream>
    #include  <string>
    //需要文件操作
    #include  <fstream>
    using  namespace  std;

    int  main()
    {
        cout  <<  "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
        cout  <<  "\t欢迎使用GnoteBook记事笔记本\t\n";
        cout  <<  "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";

        string  note,fileName;
        ofstream file;
        cout  <<  "请选择:\n";
        cout  <<  "1.新建记事本请输入1\t2.打开记事本请输入2\t";
        cout  <<  endl;
        //选择模式
        cout  <<  "请选择:";
        int  mode(0);
        cin  >>  mode;

        if(mode  !=  1)
        {
            if(mode  ==  2)
            {
                cout  <<  "此功能还未完成!敬请期待!\n";
            }
            else
            {
                //如果用户输入了无效的选择,推出
                cout  <<  "错误~~~~退出鸟~~~\n";
                return  -1;
            }
        }
        else
        {
            cout  <<  "给你的便条起个名字吧(注意:要把.txt后缀带上哦!):";
            cin  >>  fileName;
            //如果用户未输入,则用默认名称
            if(fileName ==  "")
            {
                cout  <<  "你没有输入文件名?没关系,那就使用默认的文件名gnote.txt\n";
                fileName  =  "gnote.txt";
            }

            //创建文件
            file.open(fileName.c_str(), ios::app);
            cout  <<  "请输入文本:\n";
            cout  <<  "输入完毕后,Windows用户请按ctrl+z,Linux用户和Mac用户请按ctrl+d~~~\n";
            while(getline(cin,note));
        }
        cout  <<  "你想做什么?请输入相应的编号!"  <<  endl;
        cout  <<  "1.保存文档     2.查找字符    3.字数统计"  <<  endl;
        mode  =  0;
        cin  >>  mode;

        if(mode  ==  1)
        {
            cout  <<  endl;
            cout  <<  "文件已保存在程序所在目录!!";
            //把用户输入的内容保存到文件内
            file  <<  note;
            cout  <<  endl;
        }
        else
        {
            if(mode  ==  2)
            {
                cout  <<  "请输入你要查找的字符个数(数字):"  <<  endl;
                int  word  =  0;
                cin  >>  word;
                cout  <<  "第"  <<  word  <<  "个字符是:"  <<  note[word];
                cout  <<  endl;
                cout  <<  "文件已保存在程序所在目录!!";
                //把用户输入的内容保存到文件内
                file  <<  note;
                cout  <<  endl;
            }
            else
            {
                if(mode  ==  3)
                {
                    cout  <<  "字数有(包括标点符号和空格):"  <<  note.size()  -  1;
                    cout  <<  endl;
                    cout  <<  "文件已保存在程序所在目录!!";
                    //把用户输入的内容保存到文件内
                    file  <<  note;
                    cout  <<  endl;
                }
                else
                {
                    cout  <<  "错误~~~~退出鸟~~~"  <<  endl;
                    return  -1;
                }
            }
        }
        return  0;
    }
舅舅我~~~我快碰亏了!

编程土豆 2011-10-04 12:39
再叫点人~~~~

abreto 2011-10-04 15:47
单步一下

jybox 2011-10-04 15:56
whtsky:在这种小程序上用qt......
没必要吧…
 (2011-10-04 09:55) 

这个不是QT啊

jybox 2011-10-04 15:57
我看你那个嵌套if就头晕
试试else if

outman 2011-10-04 19:12
刚做完一个关于文件操作的程序
但是现在不想看程序了
所以 我去玩游戏去鸟~

whtsky 2011-10-04 19:39
qt creator......

内容来自[手机版]

whtsky 2011-10-04 19:43
魂淡,手机版果然坑爹…lz既然选择ubuntu了就应该试着接受*nix程序员的风格,用vim或者emacs吧…    而且qt的ide也不是很好用,cb和eclipse比这个强多了啊…

内容来自[手机版]

jybox 2011-10-04 20:03
whtsky:魂淡,手机版果然坑爹…lz既然选择ubuntu了就应该试着接受*nix程序员的风格,用vim或者emacs吧…    而且qt的ide也不是很好用,cb和eclipse比这个强多了啊…
内容来自[手机版]  (2011-10-04 19:43) 

感觉QTC挺好的嘛

编程土豆 2011-10-05 10:21


调试不了~~~~郁闷
[attachment=99]

jybox 2011-10-05 15:29
编程土豆:调试不了~~~~郁闷
[图片]
 (2011-10-05 10:21) 

展示一下你的.pro文件

另外,你这个工程是建在ntfs分区还速ext分区?

编程土豆 2011-10-05 18:55
pro:
  1. SOURCES += \
        WordCount.cpp

我建在个人文件夹里的,貌似是NTFS

编程土豆 2011-10-05 18:55
pro:
  1. SOURCES += \
        WordCount.cpp

我建在个人文件夹里的,貌似是NTFS
[attachment=103]

pcliker 2011-10-05 19:44

jybox 2011-10-05 19:47
无能为力

whtsky 2011-10-05 20:33
编程土豆:pro:
SOURCES += \
    WordCount.cpp
我建在个人文件夹里的,貌似是NTFS
[图片]
....... (2011-10-05 18:55) 


littlepig 2011-12-05 13:01
C的事情别@ 我……




Powered by phpwind v8.7 Code ©2003-2011 phpwind
Time 0.049035 second(s),query:5 Gzip enabled