I would like to make a program which allows you to enter a number (say 145). It reads the 3 CHAR and prints the largest one.

I can make it to compare integers but where I am lost is how to compare characters in a single 145 (example) input

so
input 145 [enter]
print 5 (largest #)

thats what I basically want to do

Recommended Answers

All 25 Replies

You could use the atoi() function to convert from chat to int. After that, you can do the rest with some simple math.

commented: Completely worthless and cannot possibly help. -4

Use the modulus operator and division by 10 in a loop. Modulus will give you the least significant single digit of the number division will 'right shift' the value by one digit.

heyyy thanks for help :))
you have any sample code to make it easier for me??

thanks!!

Please give us your code first.

I dont know where to start :(((

I am trying to use CHAR but I am totally confused on how to differentiate each digit and compare it ://

char a, b, c, max;

    cout << "Enter a, b and c: ";
    cin >> a >> b >> c;

    max = a;
    if (b>max)
        max = b;
    if (c>max)
        max = c;
    cout << "Max is " << max << "\n";

I have this but how do I compare the input..lets say 134 and compare 1 3 4

How about this:

// test55.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

#using namespace std;

int number;
int max = 0;

cout << "Enter a number : ";
cin >> number;

while (number != 0)
{
    if ((number % 10) > max)) //Remainder of number / 10
    {
        max = (number % 10);
    }
    number /= 10;  //remove the last digit
}

cout << "The largest number was " << max;

int _tmain(int argc, _TCHAR* argv[])
{
	return 0;

Use a char array and a for loop. Store the input into the array and loop through it to find the largest number.

commented: The first post that makes any sense at all!!! +17

Use a char array and a for loop. Store the input into the array and loop through it to find the largest number.

How would I do that!?!?

Easy way is to use std::string

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main(){
 std::string input;
 cin >> input;
 for(int i = 0; i < input.size(); ++i) {
    cout << input[i] << endl;
 }

  //sort it 
  std::sort( input.begin(), input.end());
  cout << input << endl;

}
commented: You've got to be kidding!! -4

Easy way is to use std::string

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main(){
 std::string input;
 cin >> input;
 for(int i = 0; i < input.size(); ++i) {
    cout << input[i] << endl;
 }

  //sort it 
  std::sort( input.begin(), input.end());
  cout << input << endl;

}

this stuff is too advanced for me!! :((
I need if/else/when/switch basic stuff like that!!

Have you used char arrays? Have you used a for loop? If you have then think how a char array works and how you could go through it with a for loop. Once you get there then add a condition inside the for loop to check for max.

Start with this:

#include <iostream>
using std::cout;
using std::endl;

int main () {
    int value = 135742;
    cout << "Starting with: " << value << endl;
    while (value > 0) {
        int low_digit = value % 10;
        value /= 10;
        cout << "digit: " << low_digit << endl;
    }
    return 0;
}

Add to that the parts you need. Experiment a little bit. Post back with problems you are having.

we havent learned #include <algorithm>..never heard of char arrays

int low_digit = value % 10;
value /= 10;

I have no idea what that means!

I have used for loops but not arrays!! So it would be great if I could do it in Arrays..

I still have no idea what:
int low_digit = value % 10;
value /= 10;


means

:///

help!
<3

% is the modulo operator. The result is the rightmost digit in the number provided. /= is division assignment. It is equivalent to value = value / 10; which gives the integer division of a number. That is the right shift I mentioned earlier.

Alright! So how can I use that to compare all the numbers in INPUT a=135, so that it would print "5 is largest number"

Well you can use the code L7Sqr already provide and inside the while loop you would put ion a check for the max number.

Meli123,

It sounds like you're panicking! So first of all, don't worry about what seems advanced and what you do and don't understand. Try things that people here have suggested, and see what works and what doesn't. Try to change things, understanding that your program might no longer work, or might no longer even compile. Then change it back. Make the smallest possible changes each time, until you're more comfortable changing more things.

What -have- you learned so far?
For this program, are you expecting the user to enter a number while the program is running? Have you learned how to print messages out to the user, and how to read in input from the user?
Do you know how to write a loop? E.g. "while" or "for"?
Do you understand what people have suggested as far as how to get successive digits from a number?

If you know how to display output to the user, use that to display output for your own use (debugging output): take a working example, and print out messages to yourself as often in the code as you like, to see what it's doing each step of the way. If you still don't understand something specific, please post your own code (copy and paste it into the editor, then select it and click the [ CODE ] icon at the top of the editor -- this will make it look like everyone else's code contributions in this thread), and ask specific questions about what isn't clear to you.

We really are here to help!

I would like to make a program which allows you to enter a number (say 145). It reads the 3 CHAR and prints the largest one.

I have used for loops but not arrays!! So it would be great if I could do it in Arrays..

Input the number as CHARs like you already mentioned.
Use a loop on the array (sequential collection of CHARs):

char num[10] = {0};     // create the array, all defined as 0
char largest= 0;        // create the 'largest' value -- as 0

cin >> num;             // reads in all the keystrokes into the array
for (i=0; i<10; i++)
{
    if (largest < num[i]) 
    {
        largest = num[i];  // replace with the larger value
    }
}
cout << largest;

All I have basically learned is: for/if/ if else/ for loops/ and we just got to using %..so an array wouldnt be acceptable :////

we also learned bool

It is against most nature to do very fixed function programs, when they can be general but in the case of 3 letters (be they numbers or so what) you could do comparision of a and b against others until success.

pseudocode:

read characters in as a,b,c 
  if a >= b and a >= c:
    print a
  else if b >= a and b >= c:
    print b
  else:
    print c

only if statement, no arrays.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.