PROGRAMING IS NOT RUNNING !!

I can neither understand the problem with the following code nor the error message. please help me to sort it out..

I can try alternative methods , BUT I WANT TO KNOW WHERE THE ERROR IS..

I am using gcc version 4.6.3 20120306 (Red Hat 4.6.3-2) (GCC)..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

#include<iostream>
#include<cstring>
using namespace std;
class part
 {
   private:
         char part_name[30];
         int part_number;
         double cost;
   
   public:
       void get_part(char name[], int pn, double c)
                  {
               strcpy(part_name,name);
               part_number=pn;
               cost=c;
                   }
        void show_part()
                  {
          cout << "Part Name" << part_name;
          cout << "\nPart Number " << part_number;
          cout << "\n Cost= " << cost;

                   }
                   };
int main()
 {
        part p1,p2,p3,p4,p5,p6;
   
        p1.get_part("Monitor",1,5000.00);
        p2.get_part("Mother Board",2,3000.00);
        p3.get_part("Processor" ,3,7000.00);
        p4.get_part("HDD ",4,4000.00);
        p5.get_part("RAM",5,3000.00);
        p6.get_part("Cabinet & Others",6, 3000);
        
cout << "Computer Price:- \n\n";
      
       cout << p1.show_part();
       cout << p2.show_part();
       cout << p3.show_part();
       cout << p4.show.part();
       cout << p5.show_part();
       cout << p6.show.part();
  

return 0;
}
  
Last edited on
closed account (Dy7SLyTq)
line 43
Post the error messages; not everyone has the same compiler.
The errors here are:
1. passing string literals to a function taking char[] (only an error in C++11)
2. typo in variable name "P6" (should be p6)
3. attempting to output void (which is what show_part returns)
4. typo in function name show.part (should be show_part)

Here's how another compiler displays them:
test.cc:30:21: warning: conversion from string literal to 'char *' is deprecated
      [-Wdeprecated-writable-strings]
        p1.get_part("Monitor",1,5000.00);
test.cc:35:9: error: use of undeclared identifier 'P6'
        P6.get_part("Cabinet & Others",6, 3000);
        ^
test.cc:39:13: error: invalid operands to binary expression ('ostream' (aka
      'basic_ostream<char>') and 'void')
       cout << p1.show_part();
       ~~~~ ^  ~~~~~~~~~~~~~~
test.cc:44:19: error: no member named 'show' in 'part'
       cout << p6.show.part();
               ~~ ^
Last edited on
On line 36 you write P6 when you probably meant p6 (note the case).

Line 40-45, show_part() has return type void which means that the function doesn't return anything so it doesn't make sense to print it. You probably want to call the function without using cout.
1
2
3
4
5
6
p1.show_part();
p2.show_part();
p3.show_part();
p4.show_part();
p5.show_part();
p6.show_part();
I fixed the error at line number 36 (P6) ,,

BUT WHAT ABOUT THE ERROR--

passing string literals to a function taking char[] (only an error in C++11)..

Anyone please explain this.
String loterals has type const char[], that is they may not be changed. So you should declare the function as


void get_part(const char name[], int pn, double c);
Last edited on
Topic archived. No new replies allowed.