Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Rate my Calculator 1.0 code /// Rate my other projects as well!

Give me any corrections I could make, errors in the code, etc.
Thanks
CALCULATOR:
Spoiler (Click to View)

RANDOM NUMB GEN:
Spoiler (Click to View)
If div and root are using floats, why not make all operations accept float so you can add 1.5 + 0.5

also you should do
Code
cout << endl << "Enter Operation: ";
cin >> num1 >> oper >> num2;

so you can accept the data in one line e.g
"Enter Operation: 2 + 7"
Didn't think about that but I'll do that and your other suggestion as well
You could even make them templated functions. :)
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(Mar 26, 2015 06:38 PM)0xDEAD10CC Wrote:  You could even make them templated functions. :)
I've not seen anything on those before and I'm not really understanding it on my google search lol. Care to explain or a good place to learn it?
Code
template <typename T>
T add(T x, T y) { return x + y; }
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(Mar 26, 2015 07:12 PM)0xDEAD10CC Wrote:  
Code
template <typename T>
T add(T x, T y) { return x + y; }

mov eax, dword ptr [esp+4]
add eax, dword ptr[esp+8]
^^ Ignore the skid that knows how to view the disassembly.

(Mar 26, 2015 07:12 PM)0xDEAD10CC Wrote:  
Code
template <typename T>
T add(T x, T y) { return x + y; }

Note:
T can be any type that has the + operator overloaded (as well as the copy constructor being defined--with the exception of an explicit reference type).
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
Okay I'll check that out tomorrow
Anyways I did a small contact book v1 I want to add some more stuff including Delete tomorrow but I had a question why can I only enter single words not a space then last name?
for example I can enter "Alex" but I can't enter "Spoder Man"?

code:
Code
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

int main()
{
    cout << "Welcome to Contact Book v 1.0" << endl << endl << endl;
string Contactbook[15];
int numbcontact = 0;
char Question;
bool running = true;

Start:

while (running == true) {
cout << endl << "Choose a selection [N]ew Contact, [V]iew Contacts, [E]xit Program" << endl;
cin >> Question;

if (Question == 'N' || Question == 'n') {
    cout << "Enter the name of your contact: ";
    cin >> Contactbook[numbcontact];
      numbcontact++;
    system("PAUSE");
    system("CLS");
    }

else if (Question == 'V' || Question == 'v') {
    cout << "You have " << numbcontact << " contacts in your book." << endl << endl;
      for (int i = 0; i < numbcontact; i++) {
      cout << i << ". " << Contactbook[i] << endl;
    cin.ignore(); }
}

else if (Question == 'E' || Question == 'e') {
    cout << "Adios Amigo!" << endl;
    running = false;
}

else {
    cout << "invalid selection try again." << endl << endl << endl;
    goto Start;
} }
    return 0;
}
If you used std::getline() you would. The >> operator with std::cin uses whitespace as a delimiter by default.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(Mar 26, 2015 08:32 PM)0xDEAD10CC Wrote:  If you used std::getline() you would. The >> operator with std::cin uses whitespace as a delimiter by default.

I tried get line but it made an infinite loop of the original question whenever they hit a button for example N it said type name but instead of giving the option to type it immediately said the question of NVE again

I think I used getline (cin, contactbook[numbcontact]);
On my phone so capitalization might not be right in that

updated with random numb gen
I couldn't get play again to work without goto what should I do?
I tried a while statement right above my previous one should I do that somewhere else? it just kept asking the play again over again
The reason is probably because you're mixing cin >> and getline(), and you don't cleanup the input buffer properly. Your condition checks for each menu item should really be a switch statement, and you should probably have functions which do each specific thing instead of having everything within main().
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
I remember you not bad for a first calculator just try to optimize your code.
[Image: dead.gif]
(Mar 26, 2015 07:14 PM)NTTITONV37 Wrote:  mov eax, dword ptr [esp+4]
add eax, dword ptr[esp+8]

lol holy shit even ur troll code is wrong
[esp +4] is the return address not the first param


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by StaticCGMar 10, 2025 09:21 AM 2 139 Mar 10, 2025 09:21 AM
Last Post: StaticCG
[C] by C++Nov 25, 2019 12:15 PM 16 535 Nov 25, 2019 12:15 PM
Last Post: C++
[Java] by severosMar 4, 2019 03:52 PM 3 225 Mar 4, 2019 03:52 PM
Last Post: pointandlaugh