【问题标题】:how to update values using switch cases如何使用 switch case 更新值
【发布时间】:2018-10-12 06:09:50
【问题描述】:
#include <stdio.h>

#define SIZE 4

struct Employee
{
    int no; // Employee ID
    int age; // Employee Age
    double sal; // Employee Salary
};
int main() {

    int option = 0;
    struct Employee emp[SIZE] = { {0} }; // declear the array and initialize the elements to 0
    int counter = 0;
    int count = 0;
    int idstore = 0;
    double salnew = 0;

    printf("---=== EMPLOYEE DATA ===---\n\n");


    do
    {
        //print options list
        printf("1. Display Employee information\n");
        printf("2. Add Employee\n");
        printf("3. Update Employee Salary\n");
        printf("4. Remove Employee\n");
        printf("0. Exit\n\n");
        printf("Please select from the above options: ");

        // User input for the options 
        scanf("%d", &option);
        printf("\n");


        switch (option)
        {
        case 0: // Exit the program
            printf("Exiting Employee Data Program. Good Bye!!!");
            break;
        case 1: // Display employee data 

            printf("EMP ID EMP AGE EMP SALARY\n");
            printf("====== ======= ==========\n");

            for (counter = 0; counter < SIZE; counter++)
            {
                printf("%6d%9d%11.2lf\n", emp[counter].no, emp[counter].age, emp[counter].sal);
            }
            printf("\n");

            break;

        case 2: // Adding Employees

            printf("Adding Employee\n");
            printf("===============\n");

            if (count < SIZE)
            {
                printf("Enter Employee ID: ");
                scanf("%d", &emp[count].no);
                printf("Enter Employee Age: ");
                scanf("%d", &emp[count].age);
                printf("Enter Employee Salary: ");
                scanf("%lf", &emp[count].sal);
                count++;
            }
            else
            {
                printf("ERROR!!! Maximum Number of Employees Reached\n");
            }
            printf("\n");
            break;

        case 3: // Update Employee Salary

            do
            {
                printf("Update Employee Salary\n");
                printf("======================\n");
                printf("Enter Employee ID: ");
                scanf("%d", &idstore);

                for (counter = 0; counter < SIZE; counter++)
                {
                    if (idstore == emp[count].no)
                    {
                        printf("The current salary is %lf\n", &emp[count].sal);
                        printf("Enter Employee New Salary: ");
                        scanf("%lf", &emp[count].sal);
                    }
                    else
                    {
                        printf("*** ERROR: Employee ID not found ***\n");
                        printf("Enter Employee ID: ");
                        scanf("%d", &idstore);
                    }

                }

            } while (idstore = 0);
            break;

        default:
            printf("ERROR: Incorrect Option: Try Again\n\n");
        }

    } while (option != 0);


    return 0;
}

在我的情况下,我在尝试使用从我的情况 2 中输入的 ID 号码时遇到了问题,无论我在情况 3 中输入什么,它都会给出我的错误消息,并且它也不会显示我也在情况 2 中输入的工资

【问题讨论】:

  • 欢迎来到 Stack Overflow。请阅读the help pages,获取the SO tour,了解how to ask good questions,以及this question checklist。最后学习如何创建Minimal, Complete, and Verifiable Example
  • switch 语句与更新变量无关。您是否调试过您的代码并检查了您要验证的值?另外,既然这显然不是 Java 代码,为什么要标记它为 Java?
  • 另外,这个问题及其代码与 Java 有什么关系?请不要使用不相关的标签发送垃圾邮件。
  • 请更新这个问题的标签
  • 我试图根据我输入的第一个值更新薪水的值,但它只显示 000.0 而不是我输入的 id 中当前应该基于的薪水,你所说的值是什么意思即时验证反对

标签: c arrays data-structures struct


【解决方案1】:

我认为你在这里搞错了:

printf("The current salary is %lf\n", &emp[counter].sal);

你必须得到一个变量emp[counter].sal的值,而不是它的地址&amp;emp[counter].sal

【讨论】:

    猜你喜欢
    • 2021-06-20
    • 2014-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多