Reputation: 153
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!
Upvotes: 1
Views: 2575
Reputation: 73366
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.
Upvotes: 2
Reputation: 352
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);
}
Upvotes: 1
Reputation: 770
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
atoi takes a pointer, you are passing a number.
Upvotes: 1