1

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. 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.

Example Output:

Enter a series of digits with no spaces between them.

2518

The sum of those digits is 16

The highest digit is 8

The lowest digit is 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".

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;

Any help would be most appreciated!

skulltula
  • 153
  • 1
  • 3
  • 10

3 Answers3

2

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 <=. Also, the for loop syntax was wrong.

Moreover, you wanted to convert every digit to an integer, thus atoi() was not what you wanted. 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. You should check only the first n cells of the array, where n is the number of digits the user typed.

So, after reading this and this, I replaced your cin with this:

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!

The other cells, after the n first ones are not initialized, they contain garbage values!


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. 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.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • Alright thank you so much for typing all of this out! yea i'm currently in my first computer science course with no programming experience prior, so I don't know how to pass variable to functions yet. Like you said, my cin statement was the problem. After understanding what you did with cin.peek() I rewrote mine to be much more similar to what you wrote (i didn't want to just be lazy and copy and paste your entire program). Everything works 100%! – skulltula Nov 29 '14 at 03:36
  • Bravo @skulltula, this is a very good step towards the learning process. All the typing was worth it after all! (that's a reason I upvoted your question). – gsamaras Nov 29 '14 at 18:54
1
  1. for(int i=0;i<=100,i++;) should be for(int i=0;i<100;i++) <-- This may give garbage values. See Samaras' answer for a more detailed explanation

  2. atoi takes a pointer, you are passing a number.

Noel
  • 730
  • 6
  • 15
1

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);
}
warsac
  • 251
  • 1
  • 11
  • Instead of making assumptions, you could first ask and then answer. :) Also, you might want to take into account the level the user is. I don't think (s)he knows vectors. :p Also, if you don't know how to check the number of digits read, you could take a look at my answer. – gsamaras Nov 28 '14 at 22:53
  • True. I will remember to do that in the future. – warsac Nov 28 '14 at 23:07
  • Good. Now about your program, I would change the for loop inside the function to use an unsigned integer for `i`, in order the compiler not to warn. I changed the 100 in `main()` to 3, typed the digits, but nothing happened, since the way you have it, the user should input a digit and then hit enter and so on, which is not what the question mentioned. (I now saw your update where you say that the answer is not valid). – gsamaras Nov 28 '14 at 23:17
  • Well that was not part of the question when I answered it (prematurely). My answer might have been a bit half hearted. I have not compiled the code since I wrote it on my phone. I just wanted to illustrate an idea. I have upvoted your answer and I think it should be accepted by the op. – warsac Nov 28 '14 at 23:23
  • Yeah I saw the edit. Thanks. Very good that you pointed vectors, since this is C++. If your answer was still valid, I would upvote too. – gsamaras Nov 28 '14 at 23:24