#C++ Code

1 messages · Page 1 of 1 (latest)

quiet violet
#

that's just a switch() case or a bunch of if/else and std::cin >> str;

#

then you loop until 0 or <= 0

#

and you have a double for the sum=0.00f

#

that you sum += item_cost;

weary forum
#

If I do a switch case I should make it into a loop right

quiet violet
#

loop { cin; switch; sum; cout }

weary forum
#

Ok

quiet violet
#

default: cout << error_msg << endl; break;

#

you print the cout only on 0 exit

weary forum
#

Ok

#

@quiet violet how would I make a switch statement loop?

quiet violet
#

while(1) { ... switch(ch) { ... } }

weary forum
#

@quiet violet could you help me look at this code

#

So the code outputs everything correctly but it just does not output the invalid selection for some reason

quiet violet
#

copy-paste there and click share

#

you seem on the right track

weary forum
#
#

here you go sorry about that

quiet violet
#
#

works for me 🙂

#

switch is a bit cleaner

#
#

;asm c++ -O3 -fpic -Wall -Werror

#include <iostream>
using namespace std;
void storeMenu()
{                    
    cout << "1 - Soda: $1.00" << endl;
    cout << "2 - Bagel: $1.48"<< endl;
    cout << "3 - Coffee: $2.15"<< endl;
    cout << "4 - Scone: $1.89"<< endl;
    cout << "5 - Orange Juice: $1.75"<< endl;
    cout << "6 - Muffin: $0.75"<< endl;
    cout << "0 - Quit" << endl;
    cout << "Enter the item number that you want: " << endl;
}

int main(int argc, char **argv)
{
    short done = 0;
    double totalCost = 0.0d;
    storeMenu();
    while (!done)
    {
        int item = 0;
        cin >> item;
        switch(item)
        {
            case 1: totalCost += 1.00d;    break;
            case 2: totalCost += 1.48d; break;
            case 3: totalCost += 2.15d; break;
            case 4: totalCost += 1.89d; break;
            case 5: totalCost += 1.75d; break;
            case 6: totalCost += 0.75d; break;
            case 0:
                cout << "Your item/items total is: $" << totalCost << endl;
                done = 1;
                break;
            default:
                cout << "Invalid Selection" << endl;
                break;
            }
    }
    
    return 0;
}
desert walrusBOT
#
Critical error:

Embed too large.

quiet violet
#

<iostream> is too costly 😄

#
weary forum
#

lol

#

thank you

quiet violet
#

C version

#
#

;compile c

#include <stdio.h>

static inline int readInteger()
{
    int item = 0;
    for(int i = 0; i < 100; ++i)
    {
        int ch = fgetc(stdin);
        if (ch >= '0' && ch <= '9')
        {
            item = ch - '0';
            break;
        }
        else if (ch <= ' ')
        {
            continue;
        }
        else if (ch >= '!')
        {
            item = 0;
            break;
        }
    }

    return item;
}

static inline void displayLine(const char* s)
{
    if (s != NULL)
        printf("%s\n", s);
}

static inline void displayTotal(const double totalCost)
{
    printf("Your item/items total is: $%0.2lf\n", totalCost);
}

static inline void displayStoreMenu()
{
    displayLine("1 - Soda: $1.00");
    displayLine("2 - Bagel: $1.48");
    displayLine("3 - Coffee: $2.15");
    displayLine("4 - Scone: $1.89");
    displayLine("5 - Orange Juice: $1.75");
    displayLine("6 - Muffin: $0.75");
    displayLine("0 - Quit");
    displayLine("Enter the item number that you want: ");
}

static const double ITEM_COST[8] = {
    0.00d,
    1.00d,
    1.48d,
    2.15d,
    1.89d,
    1.75d,
    0.75d,
    0.00d
};

int main(int argc, char **argv)
{
    short done = 0;
    double totalCost = 0.0d;
    displayStoreMenu();
    while (!done)
    {
        const int item = readInteger();
        switch(item)
        {
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
                totalCost += ITEM_COST[item];
            break;

            case 0:
                displayTotal(totalCost);
                done = 1;
                break;

            default:
                displayLine("Invalid Selection");
                break;
            }
    }
    
    return 0;
} 
desert walrusBOT
#
Program Output
1 - Soda: $1.00
2 - Bagel: $1.48
3 - Coffee: $2.15
4 - Scone: $1.89
5 - Orange Juice: $1.75
6 - Muffin: $0.75
0 - Quit
Enter the item number that you want: 
Your item/items total is: $0.00
quiet violet
#

;asm C -O3 -Wall -Werror -fpic

#include <stdio.h>

static inline int readInteger()
{
    int item = 0;
    for(int i = 0; i < 100; ++i)
    {
        int ch = fgetc(stdin);
        if (ch >= '0' && ch <= '9')
        {
            item = ch - '0';
            break;
        }
        else if (ch <= ' ')
        {
            continue;
        }
        else if (ch >= '!')
        {
            item = 0;
            break;
        }
    }

    return item;
}

static inline void displayLine(const char* s)
{
    if (s != NULL)
        printf("%s\n", s);
}

static inline void displayTotal(const double totalCost)
{
    printf("Your item/items total is: $%0.2lf\n", totalCost);
}

static inline void displayStoreMenu()
{
    displayLine("1 - Soda: $1.00");
    displayLine("2 - Bagel: $1.48");
    displayLine("3 - Coffee: $2.15");
    displayLine("4 - Scone: $1.89");
    displayLine("5 - Orange Juice: $1.75");
    displayLine("6 - Muffin: $0.75");
    displayLine("0 - Quit");
    displayLine("Enter the item number that you want: ");
}

static const double ITEM_COST[8] = {
    0.00d,
    1.00d,
    1.48d,
    2.15d,
    1.89d,
    1.75d,
    0.75d,
    0.00d
};

int main(int argc, char **argv)
{
    short done = 0;
    double totalCost = 0.0d;
    displayStoreMenu();
    while (!done)
    {
        const int item = readInteger();
        switch(item)
        {
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
                totalCost += ITEM_COST[item];
            break;

            case 0:
                displayTotal(totalCost);
                done = 1;
                break;

            default:
                displayLine("Invalid Selection");
                break;
        }
    }
    
    return 0;
}
desert walrusBOT
#
Assembly Output Pt. 1
.LC1:
  .string "1 - Soda: $1.00"
.LC2:
  .string "2 - Bagel: $1.48"
.LC3:
  .string "3 - Coffee: $2.15"
.LC4:
  .string "4 - Scone: $1.89"
.LC5:
  .string "5 - Orange Juice: $1.75"
.LC6:
  .string "6 - Muffin: $0.75"
.LC7:
  .string "0 - Quit"
.LC8:
  .string "Enter the item number that you want: "
.LC9:
  .string "Your item/items total is: $%0.2lf\n"
.LC10:
  .string "Invalid Selection"
main:
  push r14
  lea rdi, .LC1[rip]
  xor r14d, r14d
  push r13
  lea r13, .LC10[rip]
  push r12
  lea r12, ITEM_COST[rip]
  push rbp
  push rbx
  call puts@PLT
  lea rdi, .LC2[rip]
  call puts@PLT
  lea rdi, .LC3[rip]
  call puts@PLT
  lea rdi, .LC4[rip]
  call puts@PLT
  lea rdi, .LC5[rip]
  call puts@PLT
  lea rdi, .LC6[rip]
  call puts@PLT
  lea rdi, .LC7[rip]
  call puts@PLT
  lea rdi, .LC8[rip]
  call puts@PLT
  mov rbp, QWORD PTR stdin@GOTPCREL[rip]
.L2:
  mov ebx, 100
.L7:
  mov rdi, QWORD PTR 0[rbp]
  call fgetc@PLT
  lea edx, -48[rax]
  cmp edx, 9
  jbe .L15
  cmp eax, 32
  jle .L16
.L4:

Assembly Output Pt. 2
  movq xmm0, r14
  lea rdi, .LC9[rip]
  mov eax, 1
  call printf@PLT
  pop rbx
  xor eax, eax
  pop rbp
  pop r12
  pop r13
  pop r14
  ret
.L15:
  test edx, edx
  je .L4
  sub eax, 49
  cmp eax, 5
  ja .L5
  movsx rdx, edx
  movq xmm1, r14
  addsd xmm1, QWORD PTR [r12+rdx*8]
  movq r14, xmm1
  jmp .L2
.L16:
  sub ebx, 1
  jne .L7
  jmp .L4
.L5:
  mov rdi, r13
  call puts@PLT
  jmp .L2
ITEM_COST:
  .long 0
  .long 0
  .long 0
  .long 1072693248
  .long 2061584302
  .long 1073196564
  .long 858993459
  .long 1073820467
  .long -1546188227
  .long 1073626480
  .long 0
  .long 1073479680
  .long 0
  .long 1072168960
  .long 0
  .long 0

quiet violet
#

C code is so much shorter 😄

weary forum
#

😃

quiet violet
#

;asm C -O0

#include <stdio.h>

static inline int readInteger()
{
    int item = 0;
    for(int i = 0; i < 100; ++i)
    {
        int ch = fgetc(stdin);
        if (ch >= '0' && ch <= '9')
        {
            item = ch - '0';
            break;
        }
        else if (ch <= ' ')
        {
            continue;
        }
        else if (ch >= '!')
        {
            item = 0;
            break;
        }
    }

    return item;
}

static inline void displayLine(const char* s)
{
    if (s != NULL)
        printf("%s\n", s);
}

static inline void displayTotal(const double totalCost)
{
    printf("Your item/items total is: $%0.2lf\n", totalCost);
}

static inline void displayStoreMenu()
{
    displayLine("1 - Soda: $1.00");
    displayLine("2 - Bagel: $1.48");
    displayLine("3 - Coffee: $2.15");
    displayLine("4 - Scone: $1.89");
    displayLine("5 - Orange Juice: $1.75");
    displayLine("6 - Muffin: $0.75");
    displayLine("0 - Quit");
    displayLine("Enter the item number that you want: ");
}

static const double ITEM_COST[8] = {
    0.00d,
    1.00d,
    1.48d,
    2.15d,
    1.89d,
    1.75d,
    0.75d,
    0.00d
};

int main(int argc, char **argv)
{
    short done = 0;
    double totalCost = 0.0d;
    displayStoreMenu();
    while (!done)
    {
        const int item = readInteger();
        switch(item)
        {
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
                totalCost += ITEM_COST[item];
            break;

            case 0:
                displayTotal(totalCost);
                done = 1;
                break;

            default:
                displayLine("Invalid Selection");
                break;
        }
    }
    
    return 0;
}
desert walrusBOT
#
Assembly Output Pt. 1
readInteger:
  push rbp
  mov rbp, rsp
  sub rsp, 16
  mov DWORD PTR [rbp-4], 0
  mov DWORD PTR [rbp-8], 0
  jmp .L2
.L7:
  mov rax, QWORD PTR stdin[rip]
  mov rdi, rax
  call fgetc
  mov DWORD PTR [rbp-12], eax
  cmp DWORD PTR [rbp-12], 47
  jle .L3
  cmp DWORD PTR [rbp-12], 57
  jg .L3
  mov eax, DWORD PTR [rbp-12]
  sub eax, 48
  mov DWORD PTR [rbp-4], eax
  jmp .L4
.L3:
  cmp DWORD PTR [rbp-12], 32
  jle .L9
  cmp DWORD PTR [rbp-12], 32
  jle .L6
  mov DWORD PTR [rbp-4], 0
  jmp .L4
.L9:
  nop
.L6:
  add DWORD PTR [rbp-8], 1
.L2:
  cmp DWORD PTR [rbp-8], 99
  jle .L7
.L4:
  mov eax, DWORD PTR [rbp-4]
  leave
  ret
displayLine:
  push rbp
  mov rbp, rsp
  sub rsp, 16
  mov QWORD PTR [rbp-8], rdi
  cmp QWORD PTR [rbp-8], 0
  je .L12
  mov rax, QWORD PTR [rbp-8]
  mov rdi, rax
  call puts
.L12:
  nop
  leave
  ret
.LC0:
  .string "Your item/items total is: $%0.2lf\n"
displayTotal:
  push rbp
  mov rbp, rsp
  sub rsp, 16
  movsd QWORD PTR [rbp-8], xmm0
  mov rax, QWORD PTR [rbp-8]

Assembly Output Pt. 2
  movq xmm0, rax
  mov edi, OFFSET FLAT:.LC0
  mov eax, 1
  call printf
  nop
  leave
  ret
.LC1:
  .string "1 - Soda: $1.00"
.LC2:
  .string "2 - Bagel: $1.48"
.LC3:
  .string "3 - Coffee: $2.15"
.LC4:
  .string "4 - Scone: $1.89"
.LC5:
  .string "5 - Orange Juice: $1.75"
.LC6:
  .string "6 - Muffin: $0.75"
.LC7:
  .string "0 - Quit"
.LC8:
  .string "Enter the item number that you want: "
displayStoreMenu:
  push rbp
  mov rbp, rsp
  mov edi, OFFSET FLAT:.LC1
  call displayLine
  mov edi, OFFSET FLAT:.LC2
  call displayLine
  mov edi, OFFSET FLAT:.LC3
  call displayLine
  mov edi, OFFSET FLAT:.LC4
  call displayLine
  mov edi, OFFSET FLAT:.LC5
  call displayLine
  mov edi, OFFSET FLAT:.LC6
  call displayLine
  mov edi, OFFSET FLAT:.LC7
  call displayLine
  mov edi, OFFSET FLAT:.LC8
  call displayLine
  nop
  pop rbp
  ret
ITEM_COST:
  .long 0
  .long 0
  .long 0
  .long 1072693248
  .long 2061584302
  .long 1073196564
  .long 858993459
  .long 1073820467
  .long -1546188227

Assembly Output Pt. 3
  .long 1073626480
  .long 0
  .long 1073479680
  .long 0
  .long 1072168960
  .long 0
  .long 0
.LC10:
  .string "Invalid Selection"
main:
  push rbp
  mov rbp, rsp
  sub rsp, 48
  mov DWORD PTR [rbp-36], edi
  mov QWORD PTR [rbp-48], rsi
  mov WORD PTR [rbp-2], 0
  pxor xmm0, xmm0
  movsd QWORD PTR [rbp-16], xmm0
  mov eax, 0
  call displayStoreMenu
  jmp .L16
.L19:
  mov eax, 0
  call readInteger
  mov DWORD PTR [rbp-20], eax
  cmp DWORD PTR [rbp-20], 0
  je .L17
  cmp DWORD PTR [rbp-20], 0
  js .L18
  mov eax, DWORD PTR [rbp-20]
  sub eax, 1
  cmp eax, 5
  ja .L18
  mov eax, DWORD PTR [rbp-20]
  cdqe
  movsd xmm0, QWORD PTR ITEM_COST[0+rax*8]
  movsd xmm1, QWORD PTR [rbp-16]
  addsd xmm0, xmm1
  movsd QWORD PTR [rbp-16], xmm0
  jmp .L16
.L17:
  mov rax, QWORD PTR [rbp-16]
  movq xmm0, rax
  call displayTotal
  mov WORD PTR [rbp-2], 1
  jmp .L16
.L18:
  mov edi, OFFSET FLAT:.LC10
  call displayLine
  nop
.L16:
  cmp WORD PTR [rbp-2], 0
  je .L19
  mov eax, 0
  leave
  ret

quiet violet
#

much better asm with static inline

#
#include <iostream>

using namespace std;

static inline int readInteger()
{
    int item = 0;
    cin >> item;
    return item;
}

static inline void displayLine(const char* s)
{
    cout << s << endl;
}

static inline void displayTotal(const double totalCost)
{
    cout << "Your item/items total is: $" << totalCost << endl;
}

static inline void displayStoreMenu()
{
    displayLine("1 - Soda: $1.00");
    displayLine("2 - Bagel: $1.48");
    displayLine("3 - Coffee: $2.15");
    displayLine("4 - Scone: $1.89");
    displayLine("5 - Orange Juice: $1.75");
    displayLine("6 - Muffin: $0.75");
    displayLine("0 - Quit");
    displayLine("Enter the item number that you want: ");
}

static const double ITEM_COST[8] = {
    0.00d,
    1.00d,
    1.48d,
    2.15d,
    1.89d,
    1.75d,
    0.75d,
    0.00d
};

int main(int argc, char **argv)
{
    short done = 0;
    double totalCost = 0.0d;
    displayStoreMenu();
    while (!done)
    {
        const int item = readInteger();
        switch(item)
        {
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
                totalCost += ITEM_COST[item];
            break;

            case 0:
                displayTotal(totalCost);
                done = 1;
                break;

            default:
                displayLine("Invalid Selection");
                break;
        }
    }
    
    return 0;
}


#

I prefer this version much more readable and easier to debug and modify into C 😄 code or some other programming language