C ++需要帮助 [英] C++ need help with this

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

问题描述

我刚刚开始学习c ++,我只需要一些例子来解决这个问题,以便了解用c ++编程的方法

i已尝试使用数组,但无法完成程序和我总是错了

请尽可能帮助我!!!





< b> 读入任意数量的值(读取更多值应通过输入值0来终止。然后指定当前输入的数字的数量,奇数的数量总共读了多少个数字



我尝试过:



#include< iostream>

使用命名空间std;



int main(){

int n {0};

int size1 {0};



cout<<你想要多少个数字喜欢输入<< endl;

cin>>输入;



if(输入< 1 || input> ; 100){

cout<<请求输入介于1和100之间的数字<< endl;

}



int * numbers = new int [input];

for(int i {0}; i< input; i ++){

cout<< 请输入条目<< i + 1<< <<结束;

cin>>数字[i];





}









返回0;

I'm just beginning to learn c++ and i just need some Examples to solve this one to understand the way to program in c++
i have tried with an array but couldn't finish the program and i thing im totaly wrong
Pleas help me if you can!!!


Read in as many values as you like (reading in further values should be terminated by entering the value 0. Then specify how many of the entered numbers were currently, how many odd numbers and how many numbers were read in in total

What I have tried:

#include <iostream>
using namespace std;

int main(){
int n{0};
int size1{0};

cout<< "how many number you would like to enter"<<endl;
cin>> input;

if(input <1 || input >100){
cout<< " pleas enter a Number between 1 and 100"<<endl;
}

int *numbers = new int[input];
for (int i{0};i<input;i++){
cout << "Please enter entry " << i+1 << "." << endl;
cin>>numbers[i];


}




return 0;

推荐答案

修复你的程序:

To fix your program:
using namespace std;
int main()
{
  size_t size;

  do
  {
    cout  << "how many number you would like to enter"  <<  endl;
    cin >> size;

    if(size <1 || size >100)
    {
      cout  << " pleas enter a Number between 1 and 100"  <<  endl;
    }
  } while ( size < 1 || size > 100);


  int * number = new int[size];
  for (size_t i{0};  i < size ; ++i)
  {
    cout << "Please enter entry " << (i+1) << "." << endl;
    cin>>number[i];
  }

  // here you should provide requested output
  //...

}





但是,对于这样的任务,不需要数组,只需跟踪用户输入的甚至整数和奇数的数量。



However, for such a task, an array is not needed, simply keep track of how many even integers and how many odd integers the user enters.


引用:

我试过一个数组但是无法完成程序而且我的事情总是错误的

请尽可能帮助我!!!

i have tried with an array but couldn't finish the program and i thing im totaly wrong
Pleas help me if you can!!!



您的代码没有按照您的预期行事,或者您不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里显示你的代码是什么正在做,你的任务是与它应该做的事情进行比较。

调试器里没有魔法,它没有知道你的代码是应该做的,它没有发现错误,它只是帮助你通过展示你到底是怎么回事。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行并在执行时检查变量。



此解决方案的缺点:

- 这是一个DIY,你是跟踪问题并找到根源的那个,这导致了解决方案。

这个解决方案的优点:

- 它也是一个很好的学习工具,因为它告诉你现实,你可以看到哪种期望与现实相符。



次要效果

- 你会为自己找到虫子感到自豪。

- 你的学习技巧会提高。



你应该很快就会发现什么是错的。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

1.11 - 调试你的程序(踩踏和断点)|学习C ++ [ ^ ]

调试器仅显示您的代码正在执行的操作,并且您的任务是与应该执行的操作进行比较。


Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- It is also a great learning tool because it show you reality and you can see which expectation match reality.

secondary effects
- Your will be proud of finding bugs yourself.
- Your learning skills will improve.

You should find pretty quickly what is wrong.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


我们不做你的作业:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是像你想的那么难!你不需要一个数组(因为你不知道你会读多少个数字,你不知道制作这个数字有多大!)在简单的阶段做到这一点:

读入数据,然后将其打印出来,这样您就可以看到它直接读入数字。测试一下。你需要一个循环。当它工作时,取出打印代码。

然后添加计数器:奇数,偶数将做到这一点。并添加代码以检查它是奇数还是偶数并增加相应的计数器。

然后打印出你需要的结果。

这并不困难,只需将其分成较小的位,每一点都更容易做到!



如果您遇到特定问题,请询问相关问题,我们会尽力提供帮助。但我们不打算为你做这一切!
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think! You don't need an array for this (because you don't know how many numbers you will read, you don't know how big to make the array!) Do it in easy stages:
Read in the data, and print it back out so you can see you read it right into a number. Test it. You will need a loop for this. When it works, take out the print code.
Then add counters: odd numbers, even numbers will do it. And add code to check if it's odd or even and increment the appropriate counter.
Then print the results you need.
It's not difficult, just break it into smaller bits, and each bit gets easier to do!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


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

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