Right-justifying, user-input numbers

closed account (oy721hU5)
I am prompted to input three integer numbers, one at a time. Is there a way to right-justify those numbers? I am just beginner in C. Please lend me a helping hand.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>
#include <math.h>

int main()
{
    system("cls");

    int A, B, C;

    printf("Enter Side A = " );
    scanf ("%10d", &A);

    printf("Enter Side B = " );
    scanf ("%10d", &B);


    printf("Enter Side C = " );
    scanf ("%10d", &C);

    if ( A > 0 && B > 0 && C > 0 && A+B > C && A+C > B && B+C > A )
    {
        printf("THE TRIANGLE EXISTS!");
    }
    
    else
    {
        printf("THE TRIANGLE IS NONEXISTENT :( ");
    }


}
Why do you want/need to right justify them?
closed account (oy721hU5)
To make it look nicer.
By right-justifying, do you mean to put them at the right edge of the console? If so, there is no easy way to do it, as far as I am aware. If you REALLY needed to do it, however, you could always just tap straight into the API of your operating system. For example, with windows, you could do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

WORD getInput(LPSTR &input) {
    HANDLE hStdOut, hStdIn;
    CONSOLE_SCREEN_BUFFER_INFO csbiInfo;

    LPSTR lpszPrompt = (LPSTR)"Enter a side of the triangle: ";
    CHAR chBuffer[1];
    CHAR chInput[256];
    DWORD cRead, cWritten;
    WORD wCharacters = 0;

    hStdIn = GetStdHandle(STD_INPUT_HANDLE);
    hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

    // Get Console Screen Buffer Information (size, cursor pos, etc.)
    if (!GetConsoleScreenBufferInfo(hStdOut, &csbiInfo)) {
       MessageBox(NULL, TEXT("GetConsoleScreenBufferInfo"),
            TEXT("Console Error"), MB_OK | MB_ICONERROR);
        exit(1);
    }
        
    if (!WriteFile(hStdOut, lpszPrompt,lstrlenA(lpszPrompt),
        &cWritten, NULL)) {
        MessageBox(NULL, TEXT("WriteFile"), TEXT("Console Error"),
            MB_OK | MB_ICONERROR);
        exit(1);
    }

    while (ReadFile(hStdIn, chBuffer, 1, &cRead, NULL) &&
        (chBuffer[0] != '\r' || chBuffer[0] != '\n')) {
        wCharacters += cRead;
        csbiInfo.dwCursorPosition.X = csbiInfo.dwSize.X-(1+wCharacters);

        if (csbiInfo.dwCursorPosition.X == 0 || wCharacters>255)
            break;

        if (!SetConsoleCursorPosition(hStdOut, csbiInfo.dwCursorPosition)) {
            MessageBox(NULL, TEXT("SetConsoleCursorPosition"),
                TEXT("Console Error"), MB_OK | MB_ICONERROR);
            exit(1);
        }

        if (!WriteFile(hStdOut, chBuffer, cRead,
            &cWritten, NULL)) break;
        chInput[wCharacters-1] = chBuffer[0];
    }

    csbiInfo.dwCursorPosition.X = 0;
    if (!SetConsoleCursorPosition(hStdOut, csbiInfo.dwCursorPosition)) {
        MessageBox(NULL, TEXT("SetConsoleCursorPosition"),
            TEXT("Console Error"), MB_OK | MB_ICONERROR);
        exit(1);
    }

    strcpy(input, chInput);
    
    return wCharacters;
}

int main() {
    LPSTR chText;
    getInput(chText);

    printf("\n%s", chText);
    
    exit(0);
}


EDIT:
Oops, just tested it and it appears to be a bit buggy... :)
Can't be bothered to fix it, and its probably overly complicated anyway!
Last edited on
@john1 and especially NT3,

Not sure what you mean, printf right justifies by default:

http://www.cplusplus.com/reference/cstdio/printf/


In the flag section, it has the - flag to specify left justification. Maybe you need a width specification as well, to achieve what you want? It always pays to read up on the documentation thoroughly.

Also, scanf returns a value for how many items were successfully read. You should make use of this to see if it worked, otherwise it is a recipe for a disaster.

And I would prefer to do validation on variables after they have been initialised, in order to simplify the complicated if condition. With the if condition, use the OR operator ||. This returns as soon as any of it's conditions evaluate to true - there is no need for it to evaluate all the combinations.

Line 6 in your code is non portable.

Hope this helps a bit.
Doing right-justification of output is easy using setw() and right.

But changing the way the input is displayed is probably best left for GUI programs - in my opinion.
Doesn't std::cout default to right justification as well? I mean you would only need to use std::right if changing to it from std::left or std::internal
@TheIdeasMan

OP want his program output look like
Enter Side A =   1
Enter Side B =  10
Enter Side C = 100
THE TRIANGLE IS NONEXISTENT :( 
However he can't right justify user input, so his program looks like:
Enter Side A = 1
Enter Side B = 10
Enter Side C = 100
THE TRIANGLE IS NONEXISTENT :( 

He want to right-justify the input, which AFAIK there is no portable and standard way to do.
Last edited on
Ah - I see.

Well it never ceases to amaze me the questions that get asked :+)

It seems to me that trying to justify the input is a bit of a pointless to worry about. I suppose there is something in ncurses that might work, but is it worth it?

However, outputting to a file in a particular format is a different thing, and should be easy with the advice already given.
Not quite right-justifying input, but perhaps this may suffice:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>

#define ASK(x) "Enter Side " #x " = "
#define SHOW(x) "      Side " #x " = %10d\n"
#define PUT_PAGE system("cls") /* modify depending on platform */

int main()
{
    int a, b, c ;

    PUT_PAGE ;
    printf( ASK(A) );
    scanf( "%d", &a );

    PUT_PAGE ;
    printf( SHOW(A) ASK(B), a );
    scanf( "%d", &b );

    PUT_PAGE ;
    printf( SHOW(A) SHOW(B) ASK(C), a, b );
    scanf ("%d", &c );

    PUT_PAGE ;
    printf( SHOW(A) SHOW(B) SHOW(C), a, b, c );
}
Topic archived. No new replies allowed.