Welcome to Question and Answer Board: 

I welcome you all to post your Questions of C++ Language below and my team would try to Answer your Questions of C++ programming.

Post Question

------------------------------------------------------------------------------------------------------

Title: Difference Between C and C++ Language


Name:
abhi
Question:  most of the c program is also a c++ program . is it necessory that it should produce the same result in running in both languages ? if yes ,why.if no give  

               an example?

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Answer:

As well as i understand both of the languages have the syntax difference and the concept of the program is the same in both languages. C++, as the name suggests is a superset of C.As a matter of fact, C++ can run most of C code while C cannot run C++ code.C supports procedural programming while C++ supports both procedural and object Oriented programming language. One main difference is that C++ supports operating overloading and C doe's not.

Hope this would Help...


------------------------------------------------------------------------------------------------------

Title: Structures in C++ Language


Name:
machano
Qiuestion:





The CandyBar structure contains three members. The first member holds the brand name of a candy bar. The second member holds the weight (which may have a fractional part) of the candy bar, and the third member holds the number of calories (an integer value) in the candy bar. Write a program that declares such a structure and creates a CandyBar variable called snack, initializing its members to “Mocha Munch”, 2.3, and 350, respectively. The initialization should be part of the declaration for snack. Finally, the program should display the contents of the snack variable.

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Answer:

You didn't mentioned the Language, I am posting it in C++ Language.

#include<iostream.h>
#include<conio.h>
struct CandyBar
{
    char Brand_Name[50];
    float Brand_Weight;
    int Brand_Calories;

};

void main()
{
    CandyBar snake={"Mocha Munch",2.3,350};
    cout<<"\nBrand Name     :"<<snake.Brand_Name;
    cout<<"\nBrand Weight   :"<<snake.Brand_Weight;
    cout<<"\nBrand Calories :"<<snake.Brand_Calories;
    cout<<"\n";
    getche();
}

 

This program is running perfectly on Borland and C-Free 3.5 compilers which are availables on Software's page.

------------------------------------------------------------------------------------------------------

Title: Challenge

 

Name:
machano
Qiuestion:





Write a program that asks the user to enter his or her first name and then last name, and that then constructs, stores, and displays a third string, consisting of the user’s last name followed by a comma, a space, and first name. Use char arrays and functions from the cstring header file. A sample run could look like this: Enter your first name: Flip Enter your last name: Fleming Here’s the information in a single string: Fleming, Flip.

Answer:

You didn't mentioned the Language, I am posting it in C++ Language.

#include <iostream>
#include <cstring>
#include <conio.h>

int main()
{
   char first_name[100],last_name[100],full_name[100];
   cout << "Please enter your first Name : \n";
   cin.getline(first_name,40);
   cout << "Please enter your second name: \n";
   cin.getline(last_name,40);
    strcat(full_name,last_name);
   strcat(full_name,", ");
   strcat(full_name,first_name);
   cout << "Your Full Name is:" << full_name<< "\n";
   getche();
   return 0;
}

i think  this was what you wanted. if you think that something is wrong then please rewrite you question with details that what you exactly wanted.

 

This program is running perfectly on Borland and C-Free 3.5 compilers which are availables on Software's page.

 

------------------------------------------------------------------------------------------------------

Title: Arrays in C++

 

Name:
Sobijo
Qiuestion:





Write a program that asks the user to enter up to 10 golf scores, which are to be stored in an array. You should provide a means for the user to terminate input prior to entering 10 scores. The program should display all the scores on one line and report the average score. Handle input, display, and the average calculation with three separate arrayprocessing functions.

 

Answer:



#include <iostream>
#include <cstring>
#include <conio.h>

int main()
{
   int golf_scores[10]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},sum=0,count=0;
   char ch;
   float avg;

   for(int i=0;i<10;i++)
   {
        cout<<"\nEnter the golf Scores at the "<<i<<" location in array other than -1:";
      cin>>golf_scores[i];
      while(golf_scores[i]==-1)
      {
          cout<<"\n -1 is not allowed please reenter:";
         cin>>golf_scores[i];
      }


      sum=sum+golf_scores[i];
      count+=1;
      cout<<"\nAre you want to enter more Numbers (y/n) :";
      cin>>ch;
      if(ch=='n' || ch=='N')
          break;
   }


   avg=(float)sum/count;
   cout<<"\nYour Golf Scores are given below\n";
   for(int i=0;i<10;i++)
   {
       if(golf_scores[i]!=-1)
       cout<<golf_scores[i]<<"     ";

   }
   cout<<"\n Average Scores:"<<avg;
   getche();
   return 0;
}


i think  this was what you wanted. if you think that something is wrong then please rewrite you question with details that what you exactly wanted.

 

This program is running perfectly on Borland and C-Free 3.5 compilers which are available on Software's page.

 

------------------------------------------------------------------------------------------------------

 Title: CCTYPE in C++ Language

 

Name:
Sobijo
Qiuestion:





Write a program that reads keyboard input to the @ symbol and that echoes the input except for digits, converting each uppercase character to lowercase, and vice versa. (Don’t forget the cctype family.)

Answer:



#include <iostream.h>
#include <cctype>
#include <conio.h>

int main()
{
   int i=0,j=0,k=0;
   char ch,lower[30],upper[30];
   cout<<"Enter text for analysis, type @ to stop :";
   cin>>ch;
   while(ch!='@')
   {
        if(isalpha(ch))
        {
            if(islower(ch))
            {
                lower[i]=ch;
               i++;
            }
            if(isupper(ch))
            {
                upper[j]=ch;
                j++;
            }
        }
        cin>>ch;
   }
   lower[i]='\0';
   upper[j]='\0';
   int m=0,l=0;
   cout<<"\n\tLower case\tUpper case\n";
      cout<<"\t---------------------------\n\n";
   while(lower[m]!='\0' || upper[l]!='\0')
   {
        if(m<i)
        {
            cout<<"\t\t"<<lower[m]<<"\t\t";
            m++;
        }
        if(l<j)
        {
            cout<<upper[l];
            l++;
        }
        cout<<"\n";
   }
   cout<<"Press Enter to see after conversion\n\n";
   getche();
   m=0;
   l=0;
   while(lower[m]!='\0'||upper[l]!='\0')
   {
        lower[m]=toupper(lower[m]);
        m++;
        upper[l]=tolower(upper[l]);
        l++;
   }
   m=0;
   l=0;
   cout<<"\n\tUpper case\tLower case\n";
   while(lower[m]!='\0' || upper[l]!='\0')
   {
        if(m<i)
        {
            cout<<"\t\t"<<lower[m]<<"\t\t";
            m++;
        }
        if(l<j)
        {
            cout<<upper[l];
            l++;
        }
        cout<<"\n";
   }
  
   getche();
   return 0;
}


i think  this was what you wanted. if you think that something is wrong then please rewrite you question with details that what you exactly wanted.

 

This program is running perfectly on C-Free 3.5 compilers which is availables on Software's page.

 

------------------------------------------------------------------------------------------------------ 

  Title: Searching in Character Arrays using strstr function in C++ Language

 

Name:
Sobijo
Qiuestion:





Write a program that inputs a line of text and a search string from the keyboard. Using function strstr, locate the first occurrence of the search string in the line of text, and assign the location to variable searchPtr of type char *. If the search string is found, print the reminder of the line of text beginning with the search string. Then use strstr again to locate the next occurrence of the search string in the line of text. If a second occurrence is found, print the reminder of the line of text beginning with the second occurrence. {Hint: the second call to strstr should contain the expression searchPtr + 1 as its first argument.}

Answer:



#include<iostream.h>
#include<conio.h>
#include<cstring>
void main()
{
    char line[500];
    char search[100];
    char* searchPtr=NULL;
    cout<<"\nEnter The Line of text :";
    cin.getline(line,500);
    cout<<"\nEnter The Seacrh String:";
    cin.getline(search,100);
    int check=0;
    while(check==0)
    {
        if(searchPtr==NULL)
        {
            searchPtr=strstr(line,search);
            if(searchPtr==NULL)
            {
                cout<<"\n No search found ...";
                check=1;
            }
            else
                cout<<searchPtr<<"\n\n";

        }
        else
        {
            searchPtr=strstr(searchPtr+1,search);
            if(searchPtr==NULL)
            {
                cout<<"\n No More search found ...";
                check=1;
            }
            else
                cout<<searchPtr<<"\n\n";
        }
        getche();
    }


}

i think  this was what you wanted. if you think that something is wrong then please rewrite you question with details that what you exactly wanted.

 

This program is running perfectly on C-Free 3.5 compilers which is availables on Software's page.

 

------------------------------------------------------------------------------------------------------  

  Title: String arrays in C++ (Multidimensional String Arrays)

 

Name:
Sunny
Qiuestion:


Make a C++ program using multi dimensional array (6x3) of string type and enter the names of customers in to the first column, enter the addresses in the second column and in the third column combine both the provided information of the customers and display the results, make this program using functions

 

 

Answer:



#include<iostream.h>
#include<conio.h>
#include<string>
void customers(string arr[][3]);
void address(string arr[][3]);
void combine(string arr[][3]);
void display(string arr[][3]);
void main()
{
    string array[6][3];
    customers(array);
    address(array);
    combine(array);
    display(array);
    getche();
}
void customers(string arr[][3])
{
    string name;
    for(int i=0;i<6;i++)
    {
        cout<<"\nEnter the name of the "<<(i+1)<<" customer :";
        cin>>name;
        arr[i][0]=name;
    }
}
void address(string arr[][3])
{
    string address;
    for(int i=0;i<6;i++)
    {
        cout<<"\nEnter the Address of the "<<(i+1)<<" customer :";
        cin>>address;
        arr[i][1]=address;
    }
}
void combine(string arr[][3])
{
    for(int i=0;i<6;i++)
    arr[i][2]=arr[i][0]+" "+arr[i][1];
}
void display(string arr[][3])
{
    cout<<"\n\n \tName\tAddress\t Name+Address\n";
    for(int i=0;i<6;i++)
    cout<<"\n\n \t"<<arr[i][0]<<"\t"<<arr[i][1]<<"\t"<<arr[i][2]<<"\n";
}

i think  this was what you wanted. if you think that something is wrong then please rewrite you question with details that what you exactly wanted.

 

This program is running perfectly on C-Free 3.5 compilers which is availables on Software's page.

------------------------------------------------------------------------------------------------------   

Title: Object Oriented Programming in C++

 

Name:
ahmad
Qiuestion:


Write a class employee with four data members name, id ,age , adrress.it contains the following member function. 1. The get() function is used to input values. 2. The show ()function is used to show values. 3. The edit () function is used to edit the previous record entered.

 

 

Answer:



#include<iostream.h>
#include<conio.h>
#include<cstring>
#include<string.h>
class employee
{
    private:
    int id;
    int age;
    string name;
    string address;
    public:
    employee()   //constructor
    {
        name="NIL";
        id=0;
        age=0;
        address="NIL";
    }
    void get()
    {
        cout<<"\nEnter the name of the Employee :";
        getline(cin,name);
        cout<<"\nEnter the ID of   the Employee :";
        cin>>id;
        cout<<"\nEnter the Age of  the Employee :";
        cin>>age;
        cout<<"\nEnter the Address of the Employee:";
        getline(cin,address);
    }
    void show()
    {
        cout<<"\nName   :"<<name;
        cout<<"\nID     :"<<id;
        cout<<"\nAge    :"<<age;
        cout<<"\nAddress:"<<address;
        cout<<"\n---------------------";
    }
    void update()
    {
        cout<<"\nEnter the New Information below";
        cout<<"\nName   :";
        getline(cin,name);
        cout<<"\nID     :";
        cin>>id;
        cout<<"\nAge     :";
        cin>>age;
        cout<<"\nAddress:";
        getline(cin,address);
    }
    int get_id()
    {
        return id;
    }
};
void main()
{
    employee e[10];  //Maximum 10 employees u could change it...
    int choice,count=0;
     int check=0;
    int rec_no,check1=0,temp,check2=0;
   //  int rec_no1,check3=0,check4=0,temp1;
    // int check5=0;
    do
    {
        cout<<"\n*****Main Menu*****";
        cout<<"\n1. Insert a Record ";
        cout<<"\n2. Update a Record ";
        cout<<"\n3. Show a Record ";
        cout<<"\n4. Show all Records";
        cout<<"\n5. Exit";
        cout<<"\n   Enter your choice :";
        cin>>choice;
        switch(choice)
        {
            case 1:
                e[count].get();

                cout<<"\nInsert More ? (0. For Yes and 1.For No)";
                cin>>check;
                while(check==0 && count<10)
                {
                    count=count+1;
                    if(count==10)
                    {
                        cout<<"\n Sorry You can't enter more records, Maximum 10 records allowed !";
                        break;
                    }
                    else
                    {
                        e[count].get();
                        cout<<"\nInsert More ? (0. For Yes and 1.For No)";
                        cin>>check;
                    }
                }
            break;
            case 2:
                check1=0;
                check2=0;
                cout<<"\nEnter the Id of the Employee, U want to update his/her record :";
                cin>>rec_no;
                while(check1!=(count+1))
                {
                    temp=e[check1].get_id();
                    if(temp==rec_no)
                    {
                            e[check1].update();
                        check2=1;
                    }
                    check1++;
                }
                if(check2==1)
                    cout<<"";
                else
                    cout<<"\nSorry you have entered an Invalid ID";
            break;
            case 3:

                cout<<"\nEnter the ID of the Employee of which you want to check the record :";
                cin>>rec_no;
                check1=0;
                check2=0;
                while(check1!=(count+1))
                {
                    temp=e[check1].get_id();
                    cout<<"id:"<<temp;
                    getche();
                    if(temp==rec_no)
                    {
                         e[check1].show();
                        check2=1;
                    }
                    check1++;
                }
                if(check2==1)
                    cout<<"";
                else
                    cout<<"\nSorry you have entered an Invalid ID";
            break;
            case 4:
                check1=0;
                while(check1!=(count+1))
                {
                    e[check1].show();
                            check1++;
                }
            break;
            case 5:
                cout<<"\nGood Bye!!!";
            break;
            default:
                cout<<"\nInvalid Choice Entered!!! ";

        }
    }while(choice!=5);
    getche();
   
}

i think  this was what you wanted. if you think that something is wrong then please rewrite you question with details that what you exactly wanted.

 

This program is running perfectly on Borland C++ compiler which is available on Software's page.

 ------------------------------------------------------------------------------------------------------

 

Make a free website with Yola