我的输出显示错误需要帮助 [英] My output display is wrong need help

查看:53
本文介绍了我的输出显示错误需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主菜单在显示升序和降序功能后运行两次,

我必须向上滚动才能看到。

我该如何解决?我不想上主菜单。

这里是功能代码



  int  main( int  argc, char  * argv [ ]);  //  升序和降序函数 
{
std :: ifstream input_file(< span class =code-string> stockdatabase.txt); // 文本文件
std :: string ;
std :: vector< std :: string> file_data;
if (input_file)
{
while (!input_file。 eof())
{
std :: getline(input_file, value ' \ n');
file_data.push_back( value );
}
}
int choice = 0 ;
do
{
cout<< 按1降序排列<< endl;
cout<< 按2升序<< endl;
cout<< 按3退出<< endl;
cin>>选择; // 输入选项1或2
开关(选择)
{
案例 1
std :: cout<< 按降序排序:\ n;
std :: sort(file_data.begin(),file_data.end(),DecsendingOrderSorter()); // 降序
// 现在file_data已排序。
std :: for_each(file_data.begin(),file_data.end(),RowPrinter());

break ;

case 2
std :: cout<< ; 按升序排序:\ n;
std :: sort(file_data.begin(),file_data.end(),AscendingOrderSorter()); // 升序
// 现在file_data已排序。
std :: for_each(file_data.begin(),file_data.end(),RowPrinter());
break ;

case 3
system( clear);
exitSubMenu = true ;
break ;

默认
// 此处将处理无效输入
cout<< 您输入的选项无效,请输入选项1~3。谢谢。< < endl<< ENDL;
choice = - 1 ;
system( clear);
break ;
}
} while (选择!= 3 );
}
}







错误

----------------------------------------

- 升序和降序 -

-------------------------------------- -



S / N ItemID总数量项目描述

---------------- -------------------------------------------------- ----

按1降序排列

按升序2按

按3退出

3



(这里我已按3次退出)



-------- -------------------------------------------------- ----------

- 欢迎使用仓库管理系统 -

----------------- -------------------------------------------------- -



登录成功!欢迎回来Henry Loh!



1)添加新股票

2)删除股票

3)编辑股票项目

4)搜索库存商品

5)按升序或降序列出库存商品

6)每日库存总结报告

7)每周股票总结报告

8)月度股票总结报告

9)年度股票汇总报告

10)退出计划



请输入您的选择:



这是主菜单^

----------------------------------------

- 升序和降序 -

------------------------------------- ---



S / N ItemID总数量项目说明

--------------- -------------------------------------------------- -----

按1降序排列

按升序2按

按3退出

超过这个^

解决方案

从简单的开头开始:

  int  main( int  argc, char  * argv []);  //  升序和降序函数 
{
int choice = 0 ;
do
{
cout<< 按1降序排列<< endl;
cout<< 按2升序<< endl;
cout<< 按3退出<< endl;
cin>>选择; // 输入选项1或2
开关(选择)
{
案例 1
std :: cout<< 按降序排序:\ n;
break ;

case 2
std :: cout<< ; 按升序排序:\ n;
break ;

case 3
断裂;
默认
// 此处将处理无效输入
cout<< 您输入的选项无效,请输入选项1~3。谢谢。< < endl<< ENDL;
choice = - 1 ;
break ;
}
} while (选择!= 3 );
}



运行几次以查看它是如何工作的。使用调试器逐步完成,以查看每个步骤的更改。然后逐步逐步添加其他代码片段,直到您实现了所有部分。


确定。所以...取出 if 的东西 - 你不需要它,它只会混淆问题。然后在VS中按CTRL + A然后按CTRL + E,按CTRL + F重新格式化整个文档 - 这应该修复缩进并使代码更具可读性。



下一步,你需要查看你的代码......这有点奇怪,修复缩进可能会突出显示。这甚至可以编译吗?假设您在SortDescending构造函数的中间声明Main函数的原型...



假设确实如此,首先在第一个上放置一个断点代码行,并运行您的应用程序。

当它到达断点时,依次单步执行每一行,但在你运行预期发生之前计算出来。可以?还是会发生其他事情?为什么?



看看你认为应该发生的事情显而易见之前你看到了多远。



这可能看起来很残酷,但这是一种技巧(诚实 - 它被称为基本调试),在你走得更远之前学习它是非常必要的。


My main menu runs twice after displaying my ascending and descending order function,
i have to scroll up in order to see.
how can i solve that? i don wan the main menu to come up.
here are the codes for function

int main(int argc, char *argv[]);  // ascending and descending function 
{
    std::ifstream input_file("stockdatabase.txt"); // text file
  std::string value;
  std::vector<std::string> file_data;
   if (input_file)
  {
    while (!input_file.eof())
    {
      std::getline(input_file, value, '\n');
      file_data.push_back(value);
    }
  }
    int choice = 0;
    do
    {
        cout<< "Press 1 for descending order"<<endl;
        cout<< "Press 2 for ascending order"<<endl;
        cout<< "Press 3 to exit"<<endl;
        cin >> choice; //input choice 1 or 2
        switch (choice)
        {
        case 1:
            std::cout << "Sorting in Descending Order: \n";
            std::sort(file_data.begin(), file_data.end(), DecsendingOrderSorter());  //descending
  // now file_data is sorted.
  std::for_each(file_data.begin(), file_data.end(), RowPrinter());

            break;
        
        case 2:
            std::cout << "Sorting in Ascending Order: \n";
            std::sort(file_data.begin(), file_data.end(), AscendingOrderSorter()); // ascending
  // now file_data is sorted.
  std::for_each(file_data.begin(), file_data.end(), RowPrinter());
            break;
    
        case 3:
              system("clear");
                          exitSubMenu = true;
                        break;
                        
        default:
            // Invalid inputs will be handled here
            cout << "You have entered an invalid option, please key in Options 1 ~ 3. Thank you." << endl << endl;
            choice = -1;
                system("clear");
            break;
        }
    } while (choice != 3);
}
}




ERRORS
----------------------------------------
- Ascending and Descending Order -
----------------------------------------

S/N ItemID Total Qty Item Description
----------------------------------------------------------------------
Press 1 for descending order
Press 2 for ascending order
Press 3 to exit
3

(HERE I ALREADY PRESS 3 TO EXIT)

--------------------------------------------------------------------
- Welcome to Warehouse Management System -
--------------------------------------------------------------------

Login successful! Welcome back Henry Loh!

1) Add new stock
2) Remove stock
3) Edit stock item
4) Search stock item
5) List stock items in ascending or descending order
6) Daily stock summary report
7) Weekly stock summary report
8) Monthly stock summary report
9) Yearly stock summary report
10) Quit Program

Please enter your choice:

THIS IS THE MAIN MENU^
----------------------------------------
- Ascending and Descending Order -
----------------------------------------

S/N ItemID Total Qty Item Description
----------------------------------------------------------------------
Press 1 for descending order
Press 2 for ascending order
Press 3 to exit
OVERWRITTEN BY THIS^

解决方案

Start with something simple like:

int main(int argc, char *argv[]);  // ascending and descending function 
{
    int choice = 0;
    do
    {
        cout<< "Press 1 for descending order"<<endl;
        cout<< "Press 2 for ascending order"<<endl;
        cout<< "Press 3 to exit"<<endl;
        cin >> choice; //input choice 1 or 2
        switch (choice)
        {
        case 1:
            std::cout << "Sorting in Descending Order: \n";
            break;
        
        case 2:
            std::cout << "Sorting in Ascending Order: \n";
            break;
    
        case 3:
            break;
        default:
            // Invalid inputs will be handled here
            cout << "You have entered an invalid option, please key in Options 1 ~ 3. Thank you." << endl << endl;
            choice = -1;
            break;
        }
    } while (choice != 3);
}


Run that a few times to see how it works. Step through it with your debugger to see what changes at each step. Then gradually add the other pieces of code a step at a time until you have implemented all the pieces.


OK. So... take out the if stuff - you don't need it, and it just confuses the issue. Then in VS press CTRL+A then CTRL+E,CTRL+F to reformat the whole document - this should fix the indentation and make your code more readable.

Next, you need to look at your code...which is a bit odd, and having the indentation fixed will probably highlight that. Does this even compile? Given that you are declaring the prototype of the Main function in the middle of the SortDescending constructor...

Assuming it does, start off by putting a breakpoint on the first line of code, and run your app.
When it hits the breakpoint, single step through each line in turn, but work out before you run teh line what you expect to happen. Does it? Or does something else happen? Why?

See how far you get before it is obvious that what you thought should happen didn't.

This may seem cruel, but this is a skill (honest - it's called basic debugging) and it is pretty much essential that you learn it before you go much further.


这篇关于我的输出显示错误需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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