为什么它没有在数组中采用正确的值 [英] Why it's not taking correct value in array

查看:83
本文介绍了为什么它没有在数组中采用正确的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在接受类似3 + 2 + 1的输入,我试图从字符串中获取整数,在排序之后将数组中的整数显示为3 + 2

我的输入:

3 + 2 + 1

输出显示3 + 2应为1 2 3 .....请帮助



我尝试了什么:



 # include   <   iostream  >  
使用 命名空间性病;

int main()
{

string a;
while (cin>> a)
{
int LEN = a.size();
int n = len / 2 + 1;
int c [n],j = 0 ;
for int i = 0 ; i< len; i ++)
{
char cd = a [i];
if (a [i] == ' 1' || a [i] == ' 2' || a [ i] == ' 3'
{
c [j] = a [i] - ' 0';
j ++;
}
}

sort(c,c + n);
for int i = 0 ; i< n; i ++)
cout<< a [i]<< endl;
}
// cout<< 你好,世界! << endl;
return 0 ;
}

解决方案

开始学习如何使用调试器的时间!

放一个断点在

  for  int  i =  0 ; i< len; i ++)>  

并运行您的应用。当它到达线路时,它将停止并让你控制。

按照代码,看看它到底在做什么。看看你的变量。

在你执行每一行之前(称为单步)计算出你期望它做的事情。

它做到了吗?如果是这样,继续下一步。如果没有,为什么不呢?



这是一项技能 - 可以预测足够的调试 - 在你获得100,000行代码库之前,最好在像这样的琐碎项目上学习!但是所有技能都类似你只能通过使用它来开发它 - 所以试一试看看你能找到什么!


改变来自

Quote:

sort(c,c + n);

for(int i = 0; i< n; i ++)>

cout<< a [i]<< endl;





to



 sort(c,c + n); 
for int i = 0 ; i< n; i ++)>
cout<< c [i]<< endl;


  int  n = len / 2 + 1; 
int c [n],j = 0 ;



数组的动态内存分配 c 不能这样工作。



您应该阅读 malloc 免费

malloc - C ++参考 [ ^ ]



 sort( c,c + n); 



c 正确分配给大小 n c + n 在数组之外,数组以 c + n-1


i am taking a input like 3+2+1 and i was trying to get the integers from the string taking the integers in array after sorting it's showing 3+2
my input:
3+2+1
output showing 3 + 2 which should be 1 2 3.....help please

What I have tried:

#include <iostream>
using namespace std;

int main()
{

   string a;
   while(cin>>a)
   {
       int len=a.size();
       int n=len/2+1;
       int c[n],j=0;
       for(int i=0;i<len;i++)
       {
           char cd=a[i];
           if(a[i]=='1'||a[i]=='2'||a[i]=='3')
           {
               c[j]=a[i]-'0';
               j++;
           }
       }

       sort(c,c+n);
       for(int i=0;i<n;i++)
        cout<<a[i]<<endl;
   }
    //cout << "Hello world!" << endl;
    return 0;
}

解决方案

Time to start learning how to use the debugger!
Put a breakpoint on the line

for(int i=0;i<len;i++)>

and run your app. When it reaches the line, it will stop and let you take control.
Follow the code through, and look at exactly what it is doing. Look at your variables.
Before you execute each line (called a "single step") work out what you expect it to do.
Did it do that? If so, move on to the next. If not, why not?

This is a skill - called debugging predictably enough - and it's best learned on trivial projects like this, before you get to 100,000 line code bases! But alike all skills you only develop it by using it - so give it a try and see what you can find out!


Change from

Quote:

sort(c,c+n);
for(int i=0;i<n;i++)>
cout<<a[i]<<endl;



to

sort(c,c+n);
for(int i=0;i<n;i++)>
  cout<<c[i]<<endl;


int n=len/2+1;
int c[n],j=0;


Dynamic memory allocation for array c don't work this way.

You should read about malloc and free
malloc - C++ Reference[^]

sort(c,c+n);


When c is correctly allocated to size n, c+n is outside of the array, the array ends at c+n-1.


这篇关于为什么它没有在数组中采用正确的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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