Thread: Help with Functions

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    9

    Help with Functions

    i have tried the same code without using functions ... and it worked perfectly ...

    but now when i try to use Funztions for the same programme ... it doesnt works for me ....

    i thnk there is a problem with CHAR in functions .... can some can help me ??
    Attached Files Attached Files

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Moved to C++ programming forum. Also, the code attached is:
    Code:
    /*
      Name: Haider Ali  
      Date: 16/09/12 18:08
      Description: Conversione da Binario ad Intero ; Riceve un array come parametro e restituisce il numero decimale
    */
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <iostream.h>
    #include <conio.h>
    #include <math.h>
    
    int deciimale(char parola[]);
    
    
    int main () {
             char word[20];
             int deci;
             
             //Inserimento del numero binario in una Stringa Caratteri per poi calcolare la lunghezza con strlen
             cout<<"Insert a Binary Number :  ";
             cin>>word; 
             
             deci=deciimale(word[]);
             
             printf("\n\n\n");
             cout<<"The Decimal Number for your Binary Number is :   " <<deci;
             
             
             //system("pause");
             getch();
             
    }
             
    int deciimale (char parola[]){
                 int decini;
                 int dectot=0;
                 int l;
                 int par[20];
                 int j;
                 int i;
                 
                 l=strlen(parola);
                  /*conversione della stringa dal Char in INT con i valori corrispondenti al codice ASCII
                 for (i=0;i<l;i++)
                     par[i]= (int)parola[i];*/
                 //facendo -48 si ottiene il valore 1 o 0 nel codice ASCII
                 for (i=0;i<l;i++)
                     par[i]= parola[i]-48;
        
                 //Conversione del numero binario in Decimale
                 j=l-1;
                 for (i=0;i<l;i++) {
                     decini = par[j]* pow(2,i);
                     j--;
                     dectot = dectot + decini;
                 }
                 
                 return dectot;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Now, I think you need to be clear that these include C standard headers:
    Code:
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    The C++ equivalents are:
    Code:
    #include <cstring>
    #include <cstdlib>
    #include <cstdio>
    #include <cmath>
    Unless you have special reason to use C standard I/O, you should stick with C++ standard I/O, hence you should not #include <cstdio>. Next, this includes a pre-standard header:
    Code:
    #include <iostream.h>
    The correct standard header should be included by:
    Code:
    #include <iostream>
    This includes a non-standard header:
    Code:
    #include <conio.h>
    You are using it for getch, but you don't need getch to begin with: run your command line program from a command prompt window.

    Now, in your main function, you attempt to make a function call:
    Code:
    deci=deciimale(word[]);
    However, to pass word as an argument, it should be:
    Code:
    deci = deciimale(word);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    9
    sorry the comments are all written in Italian ... i forgot to translate into English ...

    the programme is for Converting Binary Numbers into Decimal numbers ...

    if there something somebody doesnt understand ... i can help to translate it

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    9
    i use all those libraries just because im at start with these things ... and i write all the libraries i know ....

    does the C++ libraries worked perfectly also with the C language ?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by haiderali
    does the C++ libraries worked perfectly also with the C language ?
    Your program is a C++ program, or is it not?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WinAPI functions - similar functions as part of VS C++
    By jlewand in forum Windows Programming
    Replies: 2
    Last Post: 02-02-2012, 08:54 AM
  2. Creating Functions & passing information to other functions
    By RyanLeonard in forum C Programming
    Replies: 4
    Last Post: 10-28-2010, 12:17 PM
  3. Replies: 7
    Last Post: 04-19-2006, 11:17 AM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM