After finishing my lesson on loops and array initialization i managed to sit down for a good 15 min. and right a calculator now the code is perfectly clean but when i run it i doesn't do what
i programmed it to do..here is the code

//***********************
//*                                                 
//*         Calc.C             
//*                            
//*    Author: >THE REAPER<    
//*                           
//*       Description:         
//*                            
//*   This Program calculates  
//*   addition,subtraction,    
//*   and multiplacation,      
//*   division using simple    
//*   input wich very user     
//*        friendly            
//***********************

#include <stdio.h>
main()
{

      float  calc[8];  //an "array" declared for efficient use.
      char  key;
      char  exit;
      float  ansewer;  //the input is stored in a floating-point.
up:
      printf("\n..............................................................\n");
      printf(".........Welcome to the simple Calculator...........\n");
      printf("....................................................................\n");
      printf("\nWhat do you want to do?\n");
      printf("Here are the choices: \n");
      printf("\nYou could type 'a' for adding\n");
      printf("you could Type 's' for subtracting\n");
      printf("You could Type 'm' for multiplacation\n");
      printf("You could Type 'd' for dividing\n");
      printf("\nPlease tell me what you want me to do now: ");
      fflush(stdin);       //fflush statements are used thoroughly through
      scanf("%c", &key);   //the loop and for the "exit" function

         for (;;)
         {

         if (key == ('a'))    //the character "key" declared is used thoroughly
         {
         printf("\nPlease enter the 1st number to add the 2nd with: ");

         scanf("%f", &calc[1]);
         printf("\nPlease enter the 2nd number to add the 1st with: ");

         scanf("%f", &calc[0]);

         ansewer = calc[1] + calc[0];  

         printf("\nThe Ansewer is Equal to %.3f\n", ansewer);
         }
         break;

         if (key == ('s'))
         {
         printf("\nPlease enter the 1st number to subtract the 2nd with: ");
         fflush(stdin);
         scanf("%f", &calc[3]);
         printf("\nPlease enter the 2nd number to subtract the 1st with: ");
         fflush(stdin);
         scanf("%f", &calc[2]);

         ansewer = calc[3] - calc[2];

         printf("\nThe Ansewer is Equal to %.3f\n", ansewer);
         }
         break;

         if (key == ('m'))
         {
         printf("\nPlease enter the 1st number to multiply the 2nd with: ");
         fflush(stdin);
         scanf("%f", &calc[5]);
         printf("\nPlease enter the 2nd number to multiply the 1st with: ");
         fflush(stdin);
         scanf("%f", &calc[4]);

         ansewer = calc[5] * calc[4];

         printf("\nThe Ansewer is Equal to %.3f\n", ansewer);
         }
         break;

         if (key == ('d'))
         {
         printf("\nPlease enter the 1st number to divide the 2nd with: ");
         fflush(stdin);
         scanf("%f", calc[7]);
         printf("\nPlease enter the 2nd number to divide the 1st with: ");
         fflush(stdin);
         scanf("%f", calc[6]);

         ansewer = calc[7] / calc[6];

         printf("\nThe Ansewer is Equal to %.3f\n", ansewer);
         }
         break;

         if (key != 'a','s','m','d'); 
         {                             
         printf("error please try again\n");

         }
        }

       while(1)  //A "while" loop is declared for usage of the "exit" function created
       {
         printf("\nDo you want to EXIT (y/n): ");
         fflush(stdin);
         scanf("%c", &exit);

         if (exit == 'n')

         goto up;  //The "goto" statement is used to restart the program if
         break;    //the "n" character is inputed..

         printf("\nPress the ENTER key to exit");
         getchar();

        }

        fflush(stdin);  //a "fflush" declared at the end for flushing the input
        getchar();      //and finally a "getchar()" is used to exit..

}

//.......................................................................................
//..............................END OF CODE.....................................
//.......................................................................................

now if you managed to compile it and run it you will get a dos-interface..and when you input anything other than 'a' you will be prompted to exit...ive been trying to fix it today but like the usual..........i get nothing all help is appreciated

Recommended Answers

All 24 Replies

And this series will explain why you should generally avoid scanf()

never mind i debugged the code here it is if anyone wants it..

//******************************
//*                            *
//*         Calc.C             *
//*                            *
//*    Author: >THE REAPER<    *
//*                            *
//*       Description:         *
//*                            *
//*    This Program calculates *
//*    addition,subtraction,   *
//*    and multiplacation,     *
//*    division using simple   *
//*    input wich very user    *
//*         friendly           *
//******************************

#include <stdio.h>
main()
{

    float calc[8]; //an "array" declared for efficient use.
    char key;
    char exit;
    float answer; //the input is stored in a floating-point.
up:
    printf("\n.............................................................\n");
    printf(".........Welcome to the simple Calculator....................\n");
    printf(".............................................................\n");
    printf("\nWhat do you want to do?\n");
    printf("Here are the choices: \n");
    printf("\nYou could type 'a' for adding\n");
    printf("you could Type 's' for subtracting\n");
    printf("You could Type 'm' for multiplacation\n");
    printf("You could Type 'd' for dividing\n");
    printf("\nPlease tell me what you want me to do now: ");
    fflush(stdin); //fflush statements are used thoroughly through
    scanf("%c", &key); //the loop and for the "exit" function
    
    for (;;)
    {
    
        if (key == ('a')){ //the character "key" declared is used thoroughly
        
           printf("\nPlease enter the 1st number to add the 2nd with: ");
           scanf("%f", &calc[1]);
           printf("\nPlease enter the 2nd number to add the 1st with: ");
           scanf("%f", &calc[0]);
        
           answer = calc[1] + calc[0];
        
        printf("\nThe answer is Equal to %.3f\n", answer);
        break;
        }
        
        if (key == ('s'))
        {
        
           printf("\nPlease enter the 1st number to subtract the 2nd with: ");
           fflush(stdin);
           scanf("%f", &calc[3]);
           printf("\nPlease enter the 2nd number to subtract the 1st with: ");
           fflush(stdin);
           scanf("%f", &calc[2]);
        
           answer = calc[3] - calc[2];
        
        printf("\nThe answer is Equal to %.3f\n", answer);
        break;
        }
        
        if (key == ('m'))
        {
        
           printf("\nPlease enter the 1st number to multiply the 2nd with: ");
           fflush(stdin);
           scanf("%f", &calc[5]);
           printf("\nPlease enter the 2nd number to multiply the 1st with: ");
           fflush(stdin);
           scanf("%f", &calc[4]);
        
           answer = calc[5] * calc[4];
        
           printf("\nThe answer is Equal to %.3f\n", answer);
           break;
        }
        
        if (key == ('d'))
        {
        
           printf("\nPlease enter the 1st number to divide the 2nd with: ");
           fflush(stdin);
           scanf("%f", &calc[7]);
           printf("\nPlease enter the 2nd number to divide the 1st with: ");
           fflush(stdin);
           scanf("%f", &calc[6]);
           answer = calc[7] / calc[6];
        
           printf("\nThe answer is Equal to %.3f\n", answer);
           break;
        }
        
        if (key != 'a','s','m','d')
        {
           printf("error please try again\n");
           break;
        
        }

       }

        while(1)
        {
        
        printf("\nDo you want to EXIT (y/n): ");
        fflush(stdin);
        scanf("%c", &exit);
        
        if (exit == 'n'){
        
        goto up; //The "goto" statement is used to restart the program if
        break; //the "n" character is inputed..
        
        }
        else
        {
        printf("\nPress the ENTER key to exit");
        getchar();
        getchar();
        return 0;
        }
    }
}

//..............................................................................
//..............................ENDOFCODE.....................................
//..............................................................................

the most part was adding the brackets and fixing syntax error's..

>never mind i debugged the code here it is if anyone wants it..
Really? It is as worthless as it was before. Only that now I am forced to let you know and post it as a disclaimer in case someone copies it as an example.

commented: Well said. +20

> the most part was adding the brackets and fixing syntax error's..
Yet all the bugs in your brain remain.
Did you bother to read any of what others posted, or did you just decide it was too much trouble?

Aia is right, your code is broken beyond belief. It's only the grace of your magical (some would say fossil) compiler which makes it work at all.

But try using any other compiler, as some day you surely will.
Only then will you realise that your 'C knowledge' is a crock.

hey calm the fuck down im only 13 jeez you
are all such good mentor's...in fact i read the posts and i use the dev-c++ compiler..and you know what i don't care about your useless posts..next time to spam my thread unless something elegant is posted..

commented: Wrong attitude at any age. -2

hey calm the fuck down im only 13 jeez you
are all such good mentor's...in fact i read the posts and i use the dev-c++ compiler..and you know what i don't care about your useless posts..next time to spam my thread unless something elegant is posted..

You are not going to get very far with that attitude, young lad. A simple thank you would have been sufficient.
By the way, your age is not an attenuation but rather an aggravation in most cases.
We are not your teachers nor your mentors. And you would not recognize at this time, anything elegant even if it would hit your head.
If you were smart, you would say right now: "I am sorry, sir, please, could you tell me more?"
And we would not even remember what a little brat you are.

i am sorry guys..i really don't know maybe because i have about 1 months of experiance in "c"...i will be on the lookout for all of your help...(I gotta admit it you are better then me when it comes to programming)..

i am sorry guys..i really don't know maybe because i have about 1 months of experiance in "c"...i will be on the lookout for all of your help...(I gotta admit it you are better then me when it comes to programming)..

Then I would *hope* that we are better programmers, even if sometimes our manners are not quite up to an acceptable level of social etiquette.

C has an odd landscape. In a strong effort to be "machine independent", it jumps all over some very useful bits of code that has been entered into the language, by some older compilers.

Take gotoxy(x, y) for example. Simply locates the cursor in a text window, to a specific row and column, on the screen. This seems to have the effect of making C purists jump right out of their skin, in a rage. So it's dropped, and the header it's in, reviled.

Now along comes Windows OSystem. What do they have - you can guess it can't you? - Gotoxy(x, y). The difference is the G is capitalized, and it's part of Windows, now.

The practical programmer just had to wait 10 years for this from the DOS days, or listen to the "The Scream"** of the purists, or learn some complex method of poking values into the graphics memory of the system. (which of course, varied widely from one type of video mode, to another)


**The Scream is a series of nearly identical famous paintings. Google it for a look see.

There's no question that the C language standards have been very helpful over the years, and continues to be a big help. Using the C standards to belittle a young C student whose compiler isn't up to date, is nothing to be proud of in my estimation.

I use an old compiler myself, as well as a new VC compiler. I don't see any reason to change that, or to revile the older compiler. It's like standing at the curb and screaming at every old car you see on the road.

you are right..i will probably upgrade my compiler..next week but for now im sticking with the dev-c++ compiler..thank you all for your help..

Member Avatar for iamthwee

Well, actually dev-c++ is good, so no you don't need to update it.

The C language became popular from its insertion due to the great potential for portability, across many form of hardware. A standard was adopted to strive for that holy grail goal of portability. That Standard defines what is suppose to be the behavior of the language. No surprise then, that when a programmer uses the C language in a way that it is against the Standard, or has not be specified by it, [s]he is invoking undefined behavior.
And that's a two word phrase you are going to be aware from now on, if you want to continue using the language. Very important.

An example of it:
fflush() is a function defined to clear an OUTPUT stream as like when you want to keep the cursor line blinking after a question inside a printf statement.

printf( "Please, enter a number: " ); /* no newline \n here */
fflush( stdout ); /* notice that is stdout and not stdin */

When you use fflush( stdin ) you are stepping in the Undefined Behavior realm. Since stdin is INPUT stream.
Anything can happen. It might work as you intended or not; it is unpredictable until you try on your compiler. Definitely the guaranties that it will compile under another compiler or platform is not there anymore.

Why it is as popular in books and tutorials?
The popularity in the misuse of the scanf() function almost perpetuates the bad use of fflush(). They have become almost an inseparable team of woes; mention one and you have to mention the other; like Abbott and Costello or Tom and Jerry.

I could quote everything you posted, but as a sample this is sufficient.

Take gotoxy(x, y) for example. Simply locates the cursor in a text window, to a specific row and column, on the screen. This seems to have the effect of making C purists jump right out of their skin, in a rage. So it's dropped, and the header it's in, reviled.

There's a HUGE difference between portability and undefined behavior.
While portability is a desirable objective, certainly the language is very capable of doing more clever things. It is the choice of the programmer to implement or not implement something that it would work in every platform.
However if you write int_array[i] = ++i * i++; it doesn't matter in what platform world you live in, it would still be unpredictable at best.

What unpredictability?

If you have a Windows OS, Gotoxy() always works. If you have the header file conio.h, gotoxy() always works.

I've used gotoxy() on everything from DOS 3.1 through WindowsXP, using everything from a 386 cpu to an E6700.

No problems. :)

Well, except that the C purists need lots of Pepto Bismol. :) :)

For instance, I wrote a Sudoku solving program. I wanted to show a large Sudoku board, which included the list for each square, of all the possible candidate digits still viable for that square.

I also wanted to show a small board, showing what the computer was working on, right now, in near-real time. Lastly, I wanted to show the original puzzle board position.

Right away, you can't do this without introducing an annoying as hell flicker onto the screen, without using a gotoxy() (either from conio.h or from Window's Gotoxy). **

That isn't what I want. I want to *do* what I can envision for the screen, on the screen, in my programs. I don't want to be told "you can't do that because it's not in the standard, and makes C purists, sick."

Tough. They made themselves sick, they can make themselves well - they'll get over it.

With a single assert, those programmers who are coding for a screen-less system, can eliminate all that conio.h includes, and that would include any gotoxy() code lines.

Somehow, that makes me the devil, because I want to see near real time updates to specific parts of the screen, and need gotoxy()/Gotoxy(), to do it. And yes, I want a dam good looking screen, without flicker, thank you very much.

A great program, without a great interface, is just a second-class program.

**You can do this via specific memory poking, if you have learned your video memory layout. It's complex and a PITA, and VERY unportable, of course.

What unpredictability?

If you have a Windows OS, Gotoxy() always works. If you have the header file conio.h, gotoxy() always works.

I've used gotoxy() on everything from DOS 3.1 through WindowsXP, using everything from a 386 cpu to an E6700.

No problems. :)

Well, except that the C purists need lots of Pepto Bismol. :) :)

I think you read my previous post too fast.

I found your previous post passionless and somewhat ambiguous other than re-stating the obvious, regarding using conio.h and gotoxy()/Gotoxy(), which are not "undefined" with either the right OS (finally), or the right header files.

Having gotoxy() or Gotoxy() as a part of C, or of the OS, has nothing to do with variable = ++i * ++i or other such syntax nonsense, imo.

well like i said before i just have a month's experiance in the c programming language and i am not willing to stop @ any time im gonna go all the way..

I found your previous post passionless and somewhat ambiguous other than re-stating the obvious, regarding using conio.h and gotoxy()/Gotoxy(), which are not "undefined" with either the right OS (finally), or the right header files.

Having gotoxy() or Gotoxy() as a part of C, or of the OS, has nothing to do with variable = ++i * ++i or other such syntax nonsense, imo.

Heavens! Were you born this thick or are you practicing at it?
The OP's program is suffering of undefined behavior and bugs by misuse of ANSI Standard C Library functions.

You were who brought all this talk about your beloved gotoxy() function and your venerated conio.h header file, and your "chip on the shoulder concerning some religion denomination named C purists."
I tried to point out that those would be in the portability issue category if any, but not necessarily was the problem at hand with the posted code by the OP.

... regarding using conio.h and gotoxy()/Gotoxy(), which are not "undefined" with either the right OS (finally), or the right header files.

Aia is trying to inform you that there is a C/C++ Standard in place. conio.h is not and never has be a C/C++ Standard header, and is only defined in very few compilers. Therefore, use of it and any function defined in it is not portable!

AAMOF, different compilers that have defined conio.h have not implemented the same functions. And sometimes the functions that are defined that have the same name don't even do the same thing.

So, when we 'quote' the Standard it's so the poster can in fact write code that has a chance of being portable. And we will generally gripe about non-standard functions for that very reason. Using your example, with my compiler I get an error using Gotoxy() . And my compiler has conio.h

Aia is trying to inform you that there is a C/C++ Standard in place. conio.h is not and never has be a C/C++ Standard header, and is only defined in very few compilers. Therefore, use of it and any function defined in it is not portable!

AAMOF, different compilers that have defined conio.h have not implemented the same functions. And sometimes the functions that are defined that have the same name don't even do the same thing.

So, when we 'quote' the Standard it's so the poster can in fact write code that has a chance of being portable. And we will generally gripe about non-standard functions for that very reason. Using your example, with my compiler I get an error using Gotoxy() . And my compiler has conio.h

Of course you got an error, you didn't follow my posts information. :)

For Windows, you have to use Gotoxy(x,y), and include the right header file for it, of course. For conio.h, you have to use gotoxy(x,y), *not* Gotoxy(x,y).

I read and throughly understood Aia's post (although he thinks otherwise). My assertion is that the C standard should have included such a practical header as conio.h, and they should have simply selected a good conio.h file, as a part of our C standard. Then the C purists wouldn't have anything to gripe about when they see it in a program.

Every compiler may not have had the same contents or functionality in their conio.h file, but they would fall in line if conio.h was defined in, and made a part of, the C standard, before long. Borland's conio.h, for instance, would have been a good choice, imo.

I understand your issue on portability. I have several programs that were written in Turbo C, and won't run in Visual C. So I continue to run them in Turbo C's IDE, or directly, as executables in Windows. They run great! Faster than ever, with newer hardware. The very few that I've changed over to run in Visual C, have taken very little re-work.

Most of the posters here are students, who are having trouble writing a relatively simple program. Do you really think they give a tinker's damn about whether their program (if they even get it finished at all), will be fully portable, and run on Solaris, BSD, Linux, BEOS, or any of the myriad of other alternative operating systems?

Not a chance.

For Windows, you have to use Gotoxy(x,y), and include the right header file for it

Just out of curiosity, in which header can one find [B]G[/B]otoxy ?


I understand your issue on portability. I have several programs that were written in Turbo C, and won't run in Visual C. So I continue to run them in Turbo C's IDE, or directly, as executables in Windows. They run great! Faster than ever, with newer hardware. The very few that I've changed over to run in Visual C, have taken very little re-work.

Turbo C programs don't run directly under windows. They run in the Virtual DOS machine, which makes the process less efficent.

The VDM isn't included with the most recent versions of Windows either, where 3rd party emulators like DOSbox are the only way run Turbo C programs.

Turbo C programs don't run directly under windows. They run in the Virtual DOS machine, which makes the process less efficent.

The VDM isn't included with the most recent versions of Windows either, where 3rd party emulators like DOSbox are the only way run Turbo C programs.

I wouldn't know - it's in WindowsXP, and despite all the "hand-waving" of supremely relevant facts you've made, this is all transparent to the user. And much faster with the current hardware, so I neither know now care how "efficient" it is. :)

I mis-stated something about Gotoxy(), however. You need to add this, to make it work:

void Gotoxy(int x, int y) {
    COORD coord;
    coord.X = x;
    coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); 
}

and you use it then, like this. . .(just a snapshot of a large function in my chess program)


	Gotoxy(45, 15);				// move list - about 21 moves down and X 3 wide = 63 moves.
	printf("                                 ");
	Gotoxy(45, 16);
	printf("                                 ");

	for(i = 17; i < 41; i++) {
		Gotoxy(45, i);
		printf("                                 "); }

        }

The header you need for this, is windows.h.

I read and throughly understood Aia's post (although he thinks otherwise). My assertion is that the C standard should have included such a practical header as conio.h, and they should have simply selected a good conio.h file, as a part of our C standard. Then the C purists wouldn't have anything to gripe about when they see it in a program.

Every compiler may not have had the same contents or functionality in their conio.h file, but they would fall in line if conio.h was defined in, and made a part of, the C standard, before long.

Then you don't understand the original concept for C/C++. The language was designed to be completely hardware independent. conio.h requires knowledge of the hardware -- specifically a monitor and keyboard. That is the reason it's never been added to The Standard, and never should be. The designers did it that way on purpose, and that's why all the conio.h files are different. They are add-ons by the compiler designers to access the I/O devices they designed their compilers for.

Borland's conio.h, for instance, would have been a good choice, imo.

I agree. I like Borland's header quite well. Unfortunately, it's still non-standard and I will still let people know it's non-standard and should not be used.

But at the same time, I also understand that the suggestion will not be acted upon in many cases because the functions in conio.h may be necessary to write a program with the exact functionality needed, like async-keyboard input, cursor placement, etc. Heck, I even use getch() and kbhit() on a regular basis myself. But I understand the limits of using the functions and won't try to port them to a Linux box. Most posters here do not understand these limitations. But some posters try to help suggesting the use of these function when the commands are not even available to the person asking the question. That's one major reason not to suggest non-Standard code.

Most of the posters here are students, who are having trouble writing a relatively simple program. Do you really think they give a tinker's damn about whether their program (if they even get it finished at all), will be fully portable, and run on Solaris, BSD, Linux, BEOS, or any of the myriad of other alternative operating systems?

No they probably don't. But if they are planning on a programming career, they sure better give a tinker's damn. It's those folks I for one want to help most. And since I'm not psychic and don't know their intentions, I for one give answers with the career path in mind.

And to answer otherwise is doing them a major injustice because they are new and don't know the difference between bad and good advice. Heck, they don't even know whether the person that helped them is a new student or a 30-year veteran in programming. And who would you prefer to get your help from? On a forum like this it's sometimes crap-shoot -- unless you've been here a while.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.