简体   繁体   English

将char数组转换为整数数组的程序c ++

[英]program that converts a char array into an integer array c++

So my assignment for school is as follows: 因此,我的上学任务如下:

Write a program that asks the users to enter a series of single digit numbers with nothing separating them. 编写一个程序,要求用户输入一系列的数字,不要将它们分开。 Read the input as a C-string object. 将输入作为C字符串对象读取。 The program should display the sum of all the single-digit numbers in the string. 程序应显示字符串中所有一位数字的总和。 For an example, if the user enters 2518, the program should display 16, which is the sum of 2, 5, 1, and 8. The program should also display the highest and lowest digits in the string. 例如,如果用户输入2518,则程序应显示16,即2、5、1和8的总和。程序还应显示字符串中的最高位和最低位。

Example Output: 示例输出:

Enter a series of digits with no spaces between them. 输入一系列数字,中间没有空格。

2518 2518

The sum of those digits is 16 这些数字的总和是16

The highest digit is 8 最高位数是8

The lowest digit is 1 最低位数为1

Here is my code: 这是我的代码:

#include<iostream>
#include <cstdlib>
#include<cstring>
using namespace std;

char input[100];
int x[100];

void user_input(char[]);
void char_int_conversion(char[],int[]);
void lowest_highest_digit(int[]);

int main()
{
    user_input(input);
    char_int_conversion(input,x);
    lowest_highest_digit(x);


    return 0;
}

void user_input(char input[])
{
    cout<<"Enter a series of digits with no spaces between them";
    cin>>input;
}

void char_int_conversion(char input[],int x[])
{
    for(int i=0;i<=100,i++;)
        x[i]=atoi(input[i]);
}

void lowest_highest_digit(int x[])
{
    int lowest=x[0];
    int highest=x[0];
    int total=0;

    for(int i=0;i<=100,i++;)
        if(x[i]<lowest)
            lowest=x[i];
    for(int i=0;i<=100,i++;)
        if(x[i]>highest)
            highest=x[i];

    for(int i=0;i<=100,i++;)
        total = total+x[i];

    cout<<"The sum of those digits is: "<<total<<endl
        <<"The highest digit is: "<<highest<<endl
        <<"The lowest digit is: "<<lowest<<endl;
}

on line 31 where i use the atoi function to convert the char array input into the integer array x, i get an error saying argument of type"char is incompatible with parameter of type "const char". 在第31行中,我使用atoi函数将char数组输入转换为整数数组x,我收到一条错误消息,指出类型为char的参数与类型为const char的参数不兼容。

if i delete the [i] from atoi(input[i]) I can get the program to build, but all the output variable then just equal to 0; 如果我从atoi(input [i])删除[i],则可以构建程序,但是所有输出变量都等于0;

Any help would be most appreciated! 非常感激任何的帮助!

The most important fix to understand is that this: 要了解的最重要的解决方法是:

for(int i=0;i<=100,i++;)
  x[i]=atoi(input[i]);

should be this: 应该是这样的:

for (int i = 0; i < n; i++)
  x[i] = input[i] - '0';

You see, indexing of arrays starts from zero and ends in size_of_array - 1. As a result you should use < and not <= . 您会看到,数组的索引从零开始,以size_of_array-1结尾。因此,您应该使用<而不是<= Also, the for loop syntax was wrong. 另外,for循环语法是错误的。

Moreover, you wanted to convert every digit to an integer, thus atoi() was not what you wanted. 此外,您想将每个数字转换为整数,因此atoi()并不是您想要的。 Instead you should use what I wrote above, taken from this answer. 相反,你应该使用什么我上面写的,取自这个答案。


A basic problem was that you would read the whole array, all the 100 cells, regardless of how many digits the user typed. 一个基本的问题是,无论用户键入多少位,您都将读取整个数组以及所有100个单元格。 You should check only the first n cells of the array, where n is the number of digits the user typed. 您应该只检查数组的前n单元格,其中n是用户键入的位数。

So, after reading this and this , I replaced your cin with this: 所以,看完这个这个 ,我换成你cin与此:

char c;
while ((cin.peek()!='\n') && cin >> c)
{
  input[n] =  c;
  n++;
}

You see, I used n to keep track of how many digits the user typed! 您看,我使用n来跟踪用户键入的位数!

The other cells, after the n first ones are not initialized, they contain garbage values! 在第n第一个未初始化的单元之后,它们包含垃圾值!


Putting them all together you have: 将它们放在一起,您将拥有:

#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;

char input[100];
int x[100];
int n = 0;

void user_input(char[]);
void char_int_conversion(char[], int[]);
void lowest_highest_digit(int[]);

int main() {
  user_input(input);
  char_int_conversion(input, x);
  lowest_highest_digit(x);

  return 0;
}

void user_input(char input[]) {
  cout << "Enter a series of digits with no spaces between them\n";
  char c;
  while ((cin.peek()!='\n') && cin >> c)
  {
    input[n] =  c;
    n++;
  }
}

void char_int_conversion(char input[], int x[]) {
  //for (int i = 0; i <= 100, i++;)
  for (int i = 0; i < n; i++)
    x[i] = input[i] - '0';
}

void lowest_highest_digit(int x[]) {
  int lowest = x[0];
  int highest = x[0];
  int total = 0;

  for (int i = 0; i < n; i++)
    if (x[i] < lowest)
      lowest = x[i];
  for (int i = 0; i < n; i++)
    if (x[i] > highest)
      highest = x[i];

  for (int i = 0; i < n; i++)
    total = total + x[i];

  cout << "The sum of those digits is: " << total << endl
       << "The highest digit is: " << highest << endl << "The lowest digit is: "
       << lowest << endl;
}

I believe you haven't learnt yet how to pass variables to functions, that's why you use global variables. 我相信您还没有学会如何将变量传递给函数,这就是为什么要使用全局变量的原因。 If this is not the case, then get rid of the global variables. 如果不是这种情况,请摆脱全局变量。


Also, since this is C++, you might want to use a std::vector . 另外,由于这是C ++,因此您可能要使用std :: vector That way you don't have to think about how many cells your array should have, or to keep track of the digits given manually. 这样,您不必考虑数组应该有多少个单元格,也不必跟踪手动给出的数字。 See warsac's answer, which might not be valid after the edit, but presents the idea of using a vector to your problem. 请参阅warsac的答案,该答案在编辑后可能无效,但提出了使用向量解决问题的想法。

  1. for(int i=0;i<=100,i++;) should be for(int i=0;i<100;i++) <-- This may give garbage values. for(int i=0;i<=100,i++;)应该是for(int i=0;i<100;i++) <-这可能会产生垃圾值。 See Samaras' answer for a more detailed explanation 请参阅萨马拉斯的答案以获取更详细的说明

  2. atoi takes a pointer, you are passing a number. atoi需要一个指针,您正在传递一个数字。

Edit: This obviously is not valid since the question was updated. 编辑:由于问题已更新,因此这显然无效。

If you know how many numbers you want to read from the standard input you could do something like this: 如果您知道要从标准输入中读取多少个数字,可以执行以下操作:

#include <vector>
#include <iostream>

using namespace std;

void PrintLargeSmallAndSum(const vector<int>& v)
{
    if(v.empty())
        return;
    int min = v[0];
    int max = v[0];
    int sum = 0;
    for(int i = 0; i < v.size(); ++i)
    {
        sum += v[i];
        if(v[i] < min)
            min = v[i];
        if(v[i] > max)
            max = v[i];
    }
    cout<<"The sum of those digits is: "<<sum<<endl
        <<"The highest digit is: "<<max<<endl
        <<"The lowest digit is: "<<min<<endl;
}

int main()
{
    vector<int> v;
    for(int i = 0; i < 100; ++i)
    {
        int number;
        cin >> number;
        v.push_back(number);
    }
    PrintLargeSmallAndSum(v);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM