code that Repeats the process.

Pages: 12
what codes do you use in order to repeat the whole process of a program?
(P.s: i think its "for loop" or "do while" but i dont know where to place it since its in parameter passing by reference..)
help...


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
  #include<stdio.h>
#include<conio.h>
#define a 0.03
#define b 0.05
#define c 0.08

void inputInc(float *);
void calcTax(float,float *);
void dispPay(float,float);

int main (void)
{
    float inc, total;
    inputInc(&inc);
    calcTax(inc, &total);
    dispPay(inc, total);
    return 0;
}

void inputInc(float *inc)
{
     printf("Input Net Income:");
     scanf("%f",inc);
     return;
}

void calcTax(float inc, float *i)
{
     
     if (*i>=8000)
     *i=inc*a;
     
     else (*i<=15000);
     *i=(inc-8000)*b + 240;
     
     if(*i>=15000)
     *i=(inc-15000)*c + 240 +750;
     else;
     
}

void dispPay(float inc, float total)
{
     printf("\nNet income:%0.2f \nTax Payable: %0.2f",inc,total);
     getch();
     return;
}
     
    
you can enclose the body of main() inside an infinite loop.

1
2
3
4
5
6
7
8
9
// . . .
void main()
{
    while(true)
    {
        //  your program here.
    }
}
// . . . 
You could use a while(true) loop inside of main(), and then break it using an 'if' statement, if the user types in an invalid amount.

P.S did you use to program in C? The main(void) isn't really necessary in C++.
it runs but it doesnt do the program again... T_T
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
  #include<stdio.h>
#include<conio.h>
#define a 0.03
#define b 0.05
#define c 0.08

void inputInc(float *);
void calcTax(float,float *);
void dispPay(float,float);

int main (void)
{
    while(true){
    float inc, total;
    inputInc(&inc);
    calcTax(inc, &total);
    dispPay(inc, total);
    }
    return 0;
}

void inputInc(float *inc)
{
     printf("Input Net Income:");
     scanf("%f",inc);
     return;
}

void calcTax(float inc, float *i)
{

     if (*i>=8000)
     *i=inc*a;

     else (*i<=15000);
     *i=(inc-8000)*b + 240;

     if(*i>=15000)
     *i=(inc-15000)*c + 240 +750;
     else;

}

void dispPay(float inc, float total)
{
     printf("\nNet income:%0.2f \nTax Payable: %0.2f",inc,total);
     getch();
     return;
}

I got it to repeat the program. I just had to press another key after it outputted the payable tax in order to repeat the process.
Unrelated to the question but I see a problem with your program. Do you think you need this statement:

if (inc >= 8000)

instead of:

if (*i>=8000)

Your '*i' which is your global variable 'total' contains garbage.
sorry for the trouble but i solved it now and yes its in c ^_^...

i used this
#include<stdio.h>
#include<conio.h>
#define a 0.03
#define b 0.05
#define c 0.08

void inputInc(float *);
void calcTax(float,float *);
void dispPay(float,float);

int main (void)
{
float inc, total;
char choice;
do
{
inputInc(&inc);
calcTax(inc, &total);
dispPay(inc, total);
printf("Repeat the whole process?");
printf(" Y = Yes || N = No");
scanf("%c",&choice);
}
while(choice != 'N' || choice != 'n');
getch();
return 0;
}



tnx anyway...
I am not real keen on beginners routinely using infinite loops because it is perceived to be easier. It can lead to messy code if not used in the right situation. It is needed when the end condition is complicated and or dependent on other variables.

If I do need to do an infinite loop, I use for(;;) because it will work with any language that has a for loop in this format.

Instead it would be better IMO to use a while or for loop with an appropriate end condition.

There are situations where infinite loops are warranted, but not IMO because one is too lazy to come with an end condition.

@ Rechard3

It is int main() not void main() according to the standard. There are a lot of people in India who use really ancient Borland Turbo C++ 3.0, who tend to do this. It won't compile on a modern compiler.

Group of Eight is right about using void when there are no arguments / parameters for a function. AFAIK it has never been a requirement in C++.
@TheIdeasMan:
It is int main() not void main() according to the standard


actually this is what the standard specified:
Basic Concepts

3.6 Start and termination

3.6.1 Main function
...
2 An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return
type of type int, but otherwise its type is implementation-defined.
...


VC++ 98, VS 2010 support both return types (int, void).
if your compiler doesn't support both ways, then probably you should consider getting a better one.
closed account (Dy7SLyTq)
you shouldnt void main anymore. "upgrading" to a compiler that supports it isnt a good idea. we are in c++11 now and shouldnt go that far to support void
closed account (3qX21hU5)
VC++ 98, VS 2010 support both return types (int, void).
if your compiler doesn't support both ways, then probably you should consider getting a better one.


Or you should consider actually doing it the correct way and then you wouldn't have to worry about if your compiler supports it or not... Generally in C++ you should avoid non standard things in your programs.

And you are incorrect Rechard void main() is non standard.

Consider when you work on a large project with multiple members on the team (Like open source software) generally you stay away from things that only work on a certain compiler (Like void main()) because is causing unneeded problems.


Rechard3 wrote:
actually this is what the standard specified:
Basic Concepts

3.6 Start and termination

3.6.1 Main function
...
2 An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return
type of type int
, but otherwise its type is implementation-defined.
...


You kind of skipped the whole part of it saying "It shall have a return type of type int" ;p
Last edited on
Richard3 wrote:
It shall have a return type of type int, but otherwise its type is implementation-defined.

this means int main(), int main(int argc, char* argv[]) and other implementation-defined forms, such as the very common int main(int argc, char* argv[], char* envp[]) are allowed. The return type of void was never allowed in C++: void main() is prohibited by the quoted sentence.
Last edited on
closed account (Dy7SLyTq)
so then guys not to hijack the post (but it is marked as solved) but how does winmain work?
closed account (3qX21hU5)
1
2
3
4
5
6
int CALLBACK WinMain(
  _In_  HINSTANCE hInstance,
  _In_  HINSTANCE hPrevInstance,
  _In_  LPSTR lpCmdLine,
  _In_  int nCmdShow
);


Not really sure what you mean by how does winmain work.
Last edited on
closed account (Dy7SLyTq)
as the main function ie it acts as main in a program
closed account (3qX21hU5)
Still have no idea what you are asking.

But correct WinMain acts as the entry point of a windows graphical program...

Are you asking How does the compiler know to invoke WinMain instead of the standard main function?
closed account (Dy7SLyTq)
yes
closed account (3qX21hU5)
Windows CRT is responsible for that.

What actually happens is that the Microsoft C runtime library (CRT) provides an implementation of main that calls either WinMain or wWinMain.

The CRT does some additional work inside main. For example, any static initializers are called before wWinMain. Although you can tell the linker to use a different entry-point function
Last edited on
Zereo wrote:
Consider when you work on a large project with multiple members on the team...


this is the only convincing response i got.

if i may ask, does the operating system use the return value of main(), if so then how?
thx in advance.
closed account (Dy7SLyTq)
one example i can think of is lets say you use system and you only want to continue if the program executed correctly. you would test it with the return value of main
Pages: 12