如何修复收集的计算以及如何阻止程序出错? [英] How do I fix the calculations on the reciept and how to stop the program from going error?

查看:49
本文介绍了如何修复收集的计算以及如何阻止程序出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int choice;

    printf("\n");
    printf("\n");
    printf("\n");
    printf("\n");
    printf("\n");
    printf("\n");
    printf("\n");
    printf("\n");
    printf("MENU\n");
    printf("-----------------------------\n");
    printf("(1) Enter Contact Information\n");
    printf("(2) Loan Money\n");
    printf("(3) Bill\n");
    printf("(4) Exit\n");
    printf("\nPlease choose: ");
    scanf("%d", &choice);

    if(choice==1)
    {
        system("cls");
        info();
    }
    if(choice==2)
    {
        system("cls");
        loan();
    }
    if(choice==3)
    {
        system("cls");
        bill();
    }
    if(choice==4)
    {
        system("cls");
        printf("Goodbye!!!");
    }
    else
    {
        main();
    }
    return 0;
}

int info()
{
    char name[30], address[100], choice;
    int phone;


    printf("\n");
    printf("\n");
    printf("\n");
    printf("\n");
    printf("\n");
    printf("\n");
    printf("\n");
    printf("\n");

    printf("\nEnter Name: ");
    scanf(" %[^\n]s", &name);
    printf("\nEnter Address: ");
    scanf(" %[^\n]s", &address);
    printf("\nEnter Telephone #: ");
    scanf("%d", phone);

    FILE*fp;
    fp=fopen("Contacts.txt", "w");

    fprintf(fp, "%s", name);
    fprintf(fp, "%s", address);
    fprintf(fp, "%d", phone);

    fclose(fp);

    printf("Do you want to loan money(Y/N)? ");
    scanf("%c", &choice);

    if(choice=='Y'||choice=='y')
    {
        system("cls");
        loan();
    }
    if(choice=='N'||choice=='n')
    {
        system("cls");
        main();
    }
}

int loan()
{
    int money, type;
    char choice;

    printf("   LOANS           REGULAR RATES\n");
    printf("(1)3 to 6 months	6.000%\n");
    printf("(2)1 year	        7.500%\n");
    printf("(3)2 years	        8.000%\n");
    printf("(4)3 years	        8.500%\n");
    printf("(5)4 years	        9.000%\n");
    printf("(6)5 years	        10.000%\n");
    printf("\n");
    printf("Pick your type of loan: ");
    scanf("%d", &type);
    printf("\nHow much? ");
    scanf("%d", &money);

    FILE*fp;
    fp=fopen("Loan.txt", "w");

    fprintf(fp, "%d", type);
    fprintf(fp, "%d", money);

    fclose(fp);

    printf("Do you want to review bill(Y/N)? ");
    scanf("%c", &choice);

    if(choice=='Y'||choice=='y')
    {
        system("cls");
        bill();
    }
    if(choice=='N'||choice=='n')
    {
        system("cls");
        main();
    }
}

int bill()
{
    int phone, type;
    float money, time, interest, total;
    char name[30], address[100], choice;

    FILE*fi;
    fi=fopen("Info.txt", "r");

    fscanf(fi, "%s", &name);
    fscanf(fi, "%s", &address);
    fscanf(fi, "%d", &phone);

    fclose(fi);

    FILE*fl;
    fl=fopen("Loan.txt", "r");

    fscanf(fl, "%d", type);
    fscanf(fl, "%f", money);

    fclose(fl);

    printf("How many years passed after you payed?");
    scanf("%f", &time);

    if(type==1)
    {
        interest=money*.06*time;
    }
    if(type==2)
    {
        interest=money*.075*time;
    }
    if(type==3)
    {
        interest=money*.08*time;
    }
    if(type==4)
    {
        interest=money*.085*time;
    }
    if(type==5)
    {
        interest=money*.09*time;
    }
    if(type==6)
    {
        interest=money*.10*time;
    }

    printf("\nCustomer's Name: %s", name);
    printf("\nAddress: %s", address);
    printf("\nTelephone #: %d", phone);
    printf("\n\nMoney Loaned             P %.2f", money);
    printf("\nInterest                 P %.2f", interest);
    printf("\n                         ______");
    printf("\nTotal Amount Due         P %.2f", total);
}





我的尝试:



What I have tried:

well after the suggestions to research and read i came up with this program, its goal is to ask for details like name address number of a person wanting to loan and how much does he/she need also the tax put on the amount depends on the time before the amount is payed. i have problems when i press 1 it doesnt have a button to go to the main menu also when i put values and when i display the receipt it doesnt show any value any suggestions where did i go wrong and how do i correct these problems. any suggestions on the program itself and how to make it more manageable and easier is also welcome. thankyou for the help

推荐答案

这不好。

对于初学者来说,永远不要尝试从main中调用main - 这就叫做 递归 并且它根本不适用于此应用程序。 (你的老师稍后会介绍)。你不应该从其他例程调用它!

相反,使用循环:

That's not good.
For starters, never try to call main from within main - that's called recursion and it's not at all appropriate for this application. (Your teacher will get to it later). You shouldn't be calling it from other routines either!
Instead, use a loop:
do
    {
    printf("MENU\n");
    printf("-----------------------------\n");
    printf("(1) Enter Contact Information\n");
    printf("(2) Loan Money\n");
    printf("(3) Bill\n");
    printf("(4) Exit\n");
    printf("\nPlease choose: ");
    scanf("%d", &choice);
    ...
    } while (choice != 4)
printf("Goodbye!!!");

这样,代码将继续打印菜单并要求输入,直到用户决定离开。

然后在每个函数中,不要调用 main - 只需从函数返回,它将返回循环。

其次,用户可以使用相同的代码开始的每个选择:清除屏幕。

所以为什么不在用户做出选择之后,在你开始计算他选择的内容之前调用它?

That way, the code will keep printing the menu and asking for input until the user decides to leave.
Then in each of your functions, don't call main - just return from the function and it will go back into the loop.
Second, every choice the user can make starts with the same code: clear the screen.
So why not call that after the user makes his choice and before you start working out what he chose?

scanf("%d", &choice);
cls();
if (choice ...

这样,你就不会复制东西了。



如果您的老师已经涵盖切换语句,这将是一个使用它们的绝佳场所!

That way, you aren't duplicating stuff.

And if your teacher has covered switch statements yet, this would be an excellent place to use them!


你在这段代码中遇到很多问题。

你的代码缺少函数原型。

当它不是递归时避免将函数作为自身的子程序调用。

学习循环

学习不同类型分支结构的用法

不属于void类型的函数应该有一个返回值。



以下是语言作者对C和C ++参考书的链接。注意,C语言是C ++的祖先,所以知道C对C ++总是有用的。 />
C编程语言 - 维基百科,免费的百科全书 [ ^ ]

https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf [ ^ ]

http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf [ ^ ]



C ++编程语言 [ ^ ]



你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候你已经接近了一个错误。
You have many problems in this code.
Your code is missing functions prototypes.
Avoid calling a function as a subprogram of itself when it is not recursive.
Learn loops
learn the usage of different types of branching structures
Functions not of type void should have a return value.

Here is links to references books on C and C++ by the authors of the languages. Note than C is the ancestor of C++, so knowing C is always useful with C++.
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]

C++ Programing Language[^]

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.


这篇关于如何修复收集的计算以及如何阻止程序出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆