0

oh okay but arent headers just ways to assoiciatite a way of doing something with what you want to call it? so by that theory there should be a semi-standard way to clear the screen?

But using that theory, which is wrong to begin with, there must be a way to think of something and have it miraculously happen.

Maybe you need to read up on what a header file really is and what goes into one. And their limitations.

0

i always get broad definitions when i look this stuff like it says "store procedures of variables to reuse in other files" and i have looked at windows.h and conio.h its all jut ifndef, define, enddef, but it actually doesnt state any code usually....also this program works now, but i have a few problems if you guys wanna jump back to that?

0

so if i make the functions doubles 4/2 will equal 2.345e346 i dont even know what that means.....then if i do int for the functions 4+3 =0 .....why do programs start acting up when you make them longer....hah

#include<iostream>
#include<windows.h>


using namespace std;
//so this is where i have just the interface, its not really "variables"
//like the one poster mentioned
//also i havent got to class yet...


int main()
{
    //strings that will be used in the program
    string s1;
    string s2;
    double d1;
    double d2;
    int addition = (d1 + d2);
    int subtraction = (d1 - d2);
    int multiplication = (d1 * d2);
    int division = (d1 / d2);
    do
    {
        cout << "What math operation would you like to perform?\n";
        cin  >> s1;


        if((s1=="Division") || s1==("division") || (s1=="/"))
        {
            cout << "Enter the first number: ";
            cin >> d1;
            cout << "Enter the second number: ";
            cin >> d2;
            cout << "The answer is: " << division;   //these work
        }
        else if((s1=="Addition") || (s1=="addition") || (s1=="+"))
        {
            cout << "Enter the first number: ";
            cin >> d1;
            cout << "Enter the second number: ";
            cin >> d2;
            cout << "The answer is: " << addition;
        }
        else if((s1== "Subtraction") || (s1=="subtraction") || (s1=="-"))
        {
            cout << "Enter the first number: ";
            cin >> d1;
            cout << "Enter the second number: ";
            cin >> d2;
            cout << "The answer is: " << subtraction;
        }
        else if((s1== "Multiplication" || s1=="multiplication") || (s1=="*"))
        {
            cout << "Enter the first number: ";
            cin >> d1;
            cout << "Enter the second number: ";
            cin >> d2;
            cout << "The answer is: " << subtraction;
        }
        else
        {
        cout << "It seems you have mistyped something, try again\n";
        cin.ignore();
        cin.get();
        main();
        }
    }while((s2=="Y") || (s2=="y"));

    if((s2=="N") || (s2=="n"))
    {
    return 0;
    }
}
0

Leave your functions (addition, subtraction, etc.) as they are, though. Don't try to jam those into main.

After reiterating that bit of advice that I already stated, I am NOT doing anything more for you until line 65 disappears. You have got to be kidding me. In fact, since you're not listening, I'm walking away from this. I don't give up on people as a rule, but you've become an exception.

1

First of all, you do not call main(), as so many has already stated. Your OS does this for you, and you should never call it since it will just result in bad things.

And where do you assign s2? You use it, but you are not assigning it to anything so it wont work. That's why, most likely, it's not working.

Learn how to actually use loops and how to call function before you write a large program and find out it crashes without a easy to find reason.

0

edited*
seriously remove the line 65 i am beginner too but learn to listen...

about 4/2 will equal 2.345e346,

wrong i thought was 2/4 now noticed 4/2 but anyway thats the way for float numbers to show how you want them :P
you need to include <iomanip> and write cout<<fixed;
and cout<<setprecision(2); //number 2 indicates how many digits you want(you can change it).

now delete line 65 cause i am sure you didnt do it yet!!!

Edited by ntrncx: n/a

2

OK, I know I'm going to regret this, because this is already an insane thread, but here goes.

I have some general points to make:

  1. Don't call main() .
  2. main() should never be called.
  3. Remove all calls to main() from your code. Immediately.
  4. Don't call main() . Ever.
  5. When people with > 1000 posts tell you that you don't need to call main() , you probably don't need to call main() .
  6. If you find yourself thinking "I might have to call main() to do this", you don't understand what you're doing so seek help from books or wherever, but remember: you don't need to call main() .
  7. The first rule of C++ is that we don't call main() .
  8. The second rule of C++ is that we. do not. call. main() .
  9. Invest in property, since its value generally increases faster than other assets. However, whilst residing in your property, remember never to call main() .
  10. "I would do anything for love, but I won't call main() " -- meat_loaf()
  11. Mourner number 1: "How'd he die?".
    Mourner number 2: "He called main() ".
    Mourner number 1: "Did anyone ever tell him not to do that?".
    Mourner number 2: "You know, they did. He just didn't listen".
  12. Please, don't call main() in your code.

OK, I also have a serious point to make about your understanding of functions. There are basically three parts to a function definition:

  1. The return-type
  2. The function name
  3. The arguments to the function

A function definition looks like this:

int myFunction(double, unsigned);

the return type, in this case, is an int . This means that when you type the function name into your program, the computer is eventually going to create an int in its place. This int will get generated by the function at run-time. The function decides what value the int will take by using the arguments that you give it (one of type double and another of type unsigned ). So, when you have a statement like:

int a = myFunction(10.0, 1);

If your function doesn't return a value, like if it just prints a message to the console, then you say that it returns void . For example:

void myFunction(){
    std::cout << "I return void!" << std::endl;
}

OK, so here's how functions work:
The computer starts running the program and it heads right to the top of main() and starts going through it in the order that you have written it down in the source file. It's making variables in different bits of memory and adding and subtracting variables etc. When the computer reaches a point in main() where there's a call to a function, say myFunction() , it pauses the execution of main() right there. It then goes off and creates all the bits that are needed to make myFunction() work, which all take up some amount of memory. In this case, this doesn't take much effort, by the function could be much bigger and more complex than this one. The computer then starts at the top of the newly created mini-program that is myFunction() and goes through it until it gets to the end of the function. Note that myFunction() could contain a call to some other function, say foo() . In this case, the computer stops at that point in myFunction() and goes off to create all the parts that this new function needs. Remember now that the computer has in memory main() , myFunction() and the newly created foo() . So it runs through foo() and gets to the end. foo() has don its thing. All the bits that were created for foo() to work are destroyed and the return-value of foo() is inserted in myFunction() at the place where foo() was called. Now the computer is free to continue going through myFunction until it eventually reaches the end of it. At this point, all the stuff associated with myFunction is destroyed and the return value is inserted in main() at the point where myFunction was called. The computer then keeps going through main() until it gets to the end of it, or some other function is there etc.

So, why is it bad to call main() ? Imagine what happens:

  1. Start main() (use some memory)
  2. move through main()
  3. get to a call to main()
  4. Pause main()
  5. Create a new main() , in a different patch of memory (uses more memory)
  6. move through the new main()
  7. get to a call to main()
  8. Pause the new main()
  9. Create another new main() , in a different patch of memory (uses more memory)
  10. move through the newer main()
  11. get to a call to main()
  12. Pause the newer main()
  13. Create another even newer main() , in a another different patch of memory (uses more memory)
  14. ....

As you can see, before long you have a large number of main() 's, all paused and waiting for a function ( main() ) to return that's never going to, all taking up memory that's never going to be freed.

I guess it's like taking the cycle:

  • Get a clean plate
  • Put food on plate
  • Eat food
  • Wash plate

If this whole process was main() , then calling main() like you do would be like inserting another copy of this process between "Eat food" and "Wash plate", after a while you'd end up running out of clean plates because they'd all be dirty. You'd never get to the "washing up" step and the end of main() where all the plates are freed. And, not only would you not have anything to eat off, neither would anyone else in your house.

This is what you're doing to the memory of the computer when you recursively call main() like this. Eventually everything will stop working because there'll be no memory for any other programs to run.

So, please, DON'T CALL main() .

Edited by ravenous: n/a

Votes + Comments
Nicely done.
0

You can't return to main() dude. If you're calling something form main in an obect you just need to return a value that indicates the suucsess or failer of the object function. What your doing by the statement "return main()" is passing the value of the function pounter to the main() function as a return value, given that your function is probably declared as an int, then a function pointer is going to cause a compiler error.

0

Of cource I could be wrong, but I'm prety sure you can't return "To a functiion" like that because return doesn't call the fumction. It just exists the current function runing on the stack, then continues execution of what ever fuction called that one.

0

Of cource I could be wrong, but I'm prety sure you can't return "To a functiion" like that because return doesn't call the fumction. It just exists the current function runing on the stack, then continues execution of what ever fuction called that one.

I think you are a bit wrong, the function will be executed and it's return value will be returned. That's why returning main is so bad (see my post above).

-1

Actually I take it back, your not even passing the point, co's that would look like return( main );

[Prefer using brackets co's it easier to read, old c thing]

return( main() ) would return the value of an execution of the main function, which I'm betting in your program is void main()

1. You should never be explicitly calling main in a program.
2. Your function returns an int, main probably doesn't, which is why your getting a compiler error.

Sorry about the spelling I'm dyslexic and sometimes I forget to use a spell checker :)

0

@Ravanous: No I get that, I didn't look at it properly, just saw return main, and my mind when "What the ...." :)

You totally right it would execute the function, but it's a totally bad thing to do. Main should only ever execute once, and then as the entry point into the program that the system executes, unless your trying to create the weirdest program ever written :)

-1

@Ravanous: Se the post directly under yours, which I posted after realising that I'd got it wrong. The whole reason I joined this group is that I'm a bit rusty on thins co's I haven't programed in anger for a while. It's all there in my noodle, I just need to jog it back out, and get up to speed on some stuff that's totally changed in Java :)

Small tasks huh :)

Votes + Comments
When you get something wrong, you don't have to post and post and post to apologize. Just hit the EDIT button.
0

yeah i get that you guys are at least 90000 times smarter than me, the reason it seems as if i dont listen is because i take your advice and then i lose some functionality, example, with taking out system calls, now i cant clear the screen, and since it will be 5 years minimum before i ever develop on anything but windows, ehhh i think i should stick with it, but im well aware that its bad, and as for calling main, i learn by example and people have return thisfunction(); and then it either gives the value of that function or goes to what that function does, now when i built this program originally everything worked fine and i thought everything was done correctly but obviously im learning from that, but see heres where im lost now, i deleted main as....everybody said, and now if you type something in correctly it says try again and then the input doesnt matter after that it returns 0 and close, i need a new way to jump to main...pointers maybe? :/ i thought this was simple but step by step im learning its actually a little tricky, but as far as that goes im not exactly concerned about time or looking like a dumbass, like i said this is all in good fun and just trying to hone skills for class in the fall, so lets iron out these problems so i can see how its correctly done, sans the portability issue as stated prior.... also to jonsca if you'll still help? if calling function is bad why should i have left them the way they were? and i was getting compiler errors from them anyway isnt that even a much worse sign then not being "proper" ? thanks for your help all of you, im deffinitly learning a lot more that this stupid book is teaching me.

0

i am new and i learn by myself but based on book,what i have noticed is that you miss basic parts,that if you have read,you would have solve your problem in no time, main() function is different from other functions(),its the start and the end,1 time use.

ok let me explain you why that happens.
as we told you the program goes from top to bottom ok until now?
in lines 18-21 you assign the math actions to division multiplication etc right?
the moment that the assignment is happening...

for example multiplication " multiplication= (d1*d2) the exact that moment,the d1 and d2 what values they have???the answer is none!random memory!

now at line 52 that multiplication starts you take the values of d1 and d2 but you ask the program to show you the multiplication of line 20.

you understood what i mean?
the reason that you take proper answer with calling main() is cause you create a replica and in line 20 the d1 and d2 have the values that you gave them after line 52,in previous main().(i think,i am not good yet with recursive functions :P)

to understand better edit your lines 16 and 17 like double d1=5; double d2=7;and run your program again without calling main().what you get?

i still suggest you take a book or over the internet,you will see that there are ways,that you didn't knew,new horizon of things to do with many ways that will make your life easier.forums are to help with what we don't understand.
actually my book has the exact same problem as yours as exercise,but forum rules don't post code :P

no one is smarter than anyone is just study and experience nothing more at least at start :P

*edited*

Edited by ntrncx: n/a

0

as you read the program the same way executes.i see your program ends after one math operation. exactly the same...what value the s2 has?don't forget to initialize values.
where you ask if someone has to continue or not?
what was the problem with your code at end of page 5 and you changed it?except calling main() and cin.ignore after cin.get() ?
edited*

Edited by ntrncx: n/a

0

s2 was for "y" or "n" options, so that it would either start from the top or quit, but now return 0 doesnt even quit the program like i want it to, its the whole using codeblocks now, its a diff compiler and obviously way more touchy, but yeah i was under the impression that if you just didnt assign a value you could use cin to have the user assign the values? maybe this book is way old and i should just get a new one because this program is wacked out every way possible

2

To the OP...

You might want to consider starting a new thread. This one's pretty long. No one like me who hasn't been following this is going to go back through all the old posts, so no one new is going to comment probably.

As far as portability, obsolete headers, calling main, and system calls, you may not care about that (and that's your right), but pretty much everyone who DOES care about it is going to point them out and try to shove you down the right path. Sometimes that's annoying since you just want to get something that works, regardless of whether it follows good practices, but people tend not to want to make suggestions that includes known flaws because that makes them look bad to their peers. Plus people want to be able to compile and run the code you post and they often can't do that with Windows-specific headers and old headers.

As far as clearing the screen, people often think that something that common would be part of the base language and are surprised to learn it isn't. There are a lot of reasons it isn't, but that doesn't make it any less frustrating for you.

0

@VernonDozier: Totally agree, this has stopped being about a particular problem and has become about programing practice. One last thing I'd say is that I'm new to this [nearly said board then] group, but everyone starts somewhere. What you need is a good book Object oriented design with respect to C++. Design first, code next. Nuf said

1

To the OP...

You might want to consider starting a new thread. This one's pretty long. No one like me who hasn't been following this is going to go back through all the old posts, so no one new is going to comment probably.

As far as portability, obsolete headers, calling main, and system calls, you may not care about that (and that's your right), but pretty much everyone who DOES care about it is going to point them out and try to shove you down the right path. Sometimes that's annoying since you just want to get something that works, regardless of whether it follows good practices, but people tend not to want to make suggestions that includes known flaws because that makes them look bad to their peers. Plus people want to be able to compile and run the code you post and they often can't do that with Windows-specific headers and old headers.

As far as clearing the screen, people often think that something that common would be part of the base language and are surprised to learn it isn't. There are a lot of reasons it isn't, but that doesn't make it any less frustrating for you.

Actually, pointing out bad habits is less about making the people who point them out look good and more about the fact that those seemingly harmless habits lead to huge problems in many cases, and new programmers who get accustomed to these habits will usually have difficulty figuring out what the problem is, since they consider it good custom, and sure, your code may be easier to get to WORK, but by using those bad practices, you're not really learning, are you?

Votes + Comments
Yes!! Thank you.
0

Yeah, maybe I'm being a bit paranoid here, and if I am apologies, but there was no intention of "looking good". I just wanted to give good advice advice without sounding like a dick, especially given that the whole thread was deteriorating into a postmortem of the program which is pretty messed up for the guy on the other end. The real problem is that the program is not structured properly. I may not have actually programed for a while, but I've designed some pretty big systems in my time, and coded them. Yes I did make a mistake because I jumped in without thinking, but I also have fifteen years experience writing low level C/C++ multi threaded multi user networked systems that manage a massive amounts of hardware for some pretty big companies. Listing them would be "making myself look good". I thought this place was supposed to be friendly. I have to say that from what I've seen it doesn't seem all that friendly to me at all, in fact it seem totally competitive, and embodies most of the reasons I stopped writing commercial code in the first place. Frankly I can just get the information from a book.

0

Don't want to play games, just want to do what I came here for, everyone happy now :)

Edited by 1ML: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.