Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: Munchor on February 18, 2011, 05:28:18 pm

Title: C/C++ Bad Habit
Post by: Munchor on February 18, 2011, 05:28:18 pm
Hey guys,

I just had this doubt while learning C++:

Code: [Select]
#include <iostream>
#include <stdio.h> // C Library
#include <string>

using namespace std;

int fibonacci(int x)
{
if (x==0)
{
return 0;
}
else if (x==1)
{
return 1;
}
else
{
return fibonacci(x-1)+fibonacci(x-2);
}

}

void myfunc() //void functions have no 'return'
{
cout << "Hello multiple functions" << endl;
}

string dispString(string x)
{
return x;
}

int add(int a,int b)
{
return a+b;
}

int main ()
{
  cout << fibonacci(4) << endl; //Gets 4th number of fibonacci sequence
  myfunc(); //This executes myfunc()
  printf("Enter first number: ");
  int firstNumber;
  cin >> firstNumber;
  printf("Enter second number: ");
  int secondNumber;
  cin >> secondNumber;
  printf("%i",add(firstNumber,secondNumber)); //This returns the sum of the two input numbers before
  cout << dispString("\nWoah Woah\n"); // This prints dispString(string)
  printf("Hello World\n"); // 'Hello World' in C

  string s; //Declares string
  cout << "\nEnter your name: "; //Asks for name input
  cin >> s; //Gets input and stores it in variable s
  cout << "\nYour name is: " << s; //Displays variable s

  system("pause>nul"); // system("pause"); but without the text (null)
  return 0; //main returns 0
}

I started C++ half an hour ago and was wondering... Is using C in the middle of C++ a bad habit? Notice what I'm doing above.

Thanks.
Title: Re: C/C++ Bad Habit
Post by: z80man on February 18, 2011, 06:09:16 pm
I usually stick to only the C++ STL, but sometimes a function might be easier to use in the C library, and for sections that require absolute speed I will write that in asm. It does make your code a little unclear when you have both iostream and stdio, but it will not cause errors when you compile.
Title: Re: C/C++ Bad Habit
Post by: Munchor on February 19, 2011, 05:28:10 am
I usually stick to only the C++ STL, but sometimes a function might be easier to use in the C library, and for sections that require absolute speed I will write that in asm. It does make your code a little unclear when you have both iostream and stdio, but it will not cause errors when you compile.

Assembly in the middle of C++? I gotta learn that now!
Title: Re: C/C++ Bad Habit
Post by: Binder News on February 19, 2011, 10:18:48 am
Yup, it's available in C as well. I just forget how...
Title: Re: C/C++ Bad Habit
Post by: Munchor on February 19, 2011, 12:37:40 pm
Yup, it's available in C as well. I just forget how...

I just googled and found out. Looks pretty decent ;D
Title: Re: C/C++ Bad Habit
Post by: Ashbad on February 20, 2011, 06:09:49 pm
in case someone else wants to know, here is an example of how to use C i the middle of a C++ program:

Code: [Select]
// This is C++ code
 extern "C" {
          //this is C code
 }
Title: Re: C/C++ Bad Habit
Post by: Munchor on February 21, 2011, 07:33:38 am
in case someone else wants to know, here is an example of how to use C i the middle of a C++ program:

Code: [Select]
// This is C++ code
 extern "C" {
          //this is C code
 }

Thanks much :D