Omnimaga
General Discussion => Technology and Development => Computer Programming => Topic started by: Munchor on February 18, 2011, 05:28:18 pm
-
Hey guys,
I just had this doubt while learning C++:
#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.
-
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.
-
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!
-
Yup, it's available in C as well. I just forget how...
-
Yup, it's available in C as well. I just forget how...
I just googled and found out. Looks pretty decent ;D
-
in case someone else wants to know, here is an example of how to use C i the middle of a C++ program:
// This is C++ code
extern "C" {
//this is C code
}
-
in case someone else wants to know, here is an example of how to use C i the middle of a C++ program:
// This is C++ code
extern "C" {
//this is C code
}
Thanks much :D