intmain(){getNames();// ask user to enter a bunch of namesdoMaintenance();// do some random stuffsortNames();// sort them in alphabetical orderprintNames();// print the sorted list of namesreturn0;}
intmain(){getNames();// ask user to enter a bunch of names// doMaintenance(); // do some random stuffsortNames();// sort them in alphabetical orderprintNames();// print the sorted list of namesreturn0;}
#include<iostream>intadd(intx,inty){returnx+y;}voidprintResult(intz){std::cout<<"The answer is: "<<z<<'\n';}intgetUserInput(){std::cout<<"Enter a number: ";intx{};std::cin>>x;returnx;}intmain(){intx{getUserInput()};inty{getUserInput()};std::cout<<x<<" + "<<y<<'\n';intz{add(x,5)};printResult(z);return0;}
输出结果不正确:
1234
Enter a number: 4
Enter a number: 3
4 + 3
The answer is: 9
#include<iostream>intadd(intx,inty){returnx+y;}voidprintResult(intz){std::cout<<"The answer is: "<<z<<'\n';}intgetUserInput(){std::cout<<"Enter a number: ";intx{};std::cin>>x;return--x;}intmain(){intx{getUserInput()};inty{getUserInput()};intz{add(x,y)};printResult(z);return0;}
输出结果:
123
Enter a number: 4
Enter a number: 3
The answer is: 5
#include<iostream>intadd(intx,inty){std::cerr<<"add() called (x="<<x<<", y="<<y<<")\n";returnx+y;}voidprintResult(intz){std::cerr<<"printResult() called (z="<<z<<")\n";std::cout<<"The answer is: "<<z<<'\n';}intgetUserInput(){std::cerr<<"getUserInput() called\n";std::cout<<"Enter a number: ";intx{};std::cin>>x;return--x;}intmain(){std::cerr<<"main() called\n";intx{getUserInput()};std::cerr<<"main::x = "<<x<<'\n';inty{getUserInput()};std::cerr<<"main::y = "<<y<<'\n';intz{add(x,y)};std::cerr<<"main::z = "<<z<<'\n';printResult(z);return0;}
现在让我们用相同的输入再次运行程序:
1 2 3 4 5 6 7 8 91011
main() called
getUserInput() called
Enter a number: 4
main::x = 3
getUserInput() called
Enter a number: 3
main::y = 2
add() called (x=3, y=2)
main::z = 5
printResult() called (z=5)
The answer is: 5
这样一来就可以看出问题了:用户输入的值是 4 ,但是main 函数中 x 得到的值却是 3。在用户输入的位置和给x赋值之间的代码肯定出了问题。在函数 getUserInput 中添加一些调试代码来,确定程序是否从用户那里获得了正确的值:
#include<iostream>intadd(intx,inty){std::cerr<<"add() called (x="<<x<<", y="<<y<<")\n";returnx+y;}voidprintResult(intz){std::cerr<<"printResult() called (z="<<z<<")\n";std::cout<<"The answer is: "<<z<<'\n';}intgetUserInput(){std::cerr<<"getUserInput() called\n";std::cout<<"Enter a number: ";intx{};std::cin>>x;std::cerr<<"getUserInput::x = "<<x<<'\n';// 添加调试信息return--x;}intmain(){std::cerr<<"main() called\n";intx{getUserInput()};std::cerr<<"main::x = "<<x<<'\n';inty{getUserInput()};std::cerr<<"main::y = "<<y<<'\n';intz{add(x,y)};std::cerr<<"main::z = "<<z<<'\n';printResult(z);return0;}
输出:
1 2 3 4 5 6 7 8 910111213
main() called
getUserInput() called
Enter a number: 4
getUserInput::x = 4
main::x = 3
getUserInput() called
Enter a number: 3
getUserInput::x = 3
main::y = 2
add() called (x=3, y=2)
main::z = 5
printResult() called (z=5)
The answer is: 5
添加这行调试代码后,我们可以看到用户输入被正确地接收到getUserInput()的变量 x 中。但是变量x却得到了错误的值。问题肯定在这两点之间。唯一的罪魁祸首是函数 getUserInput()的返回值:
#include<iostream>intadd(intx,inty){std::cerr<<"add() called (x="<<x<<", y="<<y<<")\n";returnx+y;}voidprintResult(intz){std::cerr<<"printResult() called (z="<<z<<")\n";std::cout<<"The answer is: "<<z<<'\n';}intgetUserInput(){std::cerr<<"getUserInput() called\n";std::cout<<"Enter a number: ";intx{};std::cin>>x;std::cerr<<"getUserInput::x = "<<x<<'\n';returnx;// removed -- before x}intmain(){std::cerr<<"main() called\n";intx{getUserInput()};std::cerr<<"main::x = "<<x<<'\n';inty{getUserInput()};std::cerr<<"main::y = "<<y<<'\n';intz{add(x,y)};std::cerr<<"main::z = "<<z<<'\n';printResult(z);return0;}
输出:
1 2 3 4 5 6 7 8 910111213
main() called
getUserInput() called
Enter a number: 4
getUserInput::x = 4
main::x = 4
getUserInput() called
Enter a number: 3
getUserInput::x = 3
main::y = 3
add() called (x=4, y=3)
main::z = 7
printResult() called (z=7)
The answer is: 7