Comparing char values in an array using 'if'

This is a discussion on Comparing char values in an array using 'if' within the C++ Programming forums, part of the General Programming Boards category; I'm a student (almost finished with my semester) and I'm having a problem using an if statement to compare a ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    8

    Comparing char values in an array using 'if'

    I'm a student (almost finished with my semester) and I'm having a problem using an if statement to compare a value in an array. The book I'm using lacks specific information on 2-dimensional character arrays so I'm turning to this board as a source for help. The assignment is to create a program using class inheritance (which I'm not sure I did correctly but I'm not getting errors and it appears to be functioning the way I want). The only problem is that when I try to determine if the user enters a famous artist, the program will not assign a 25000 value to the amount field. Any suggestions?

    Code:
    // Assignment:
    // Create a Painting class that holds the painting title, artist name, and value. 
    // All paintings are valued at $400 unless they are FamousPaintings.  The FamousPainting
    // subclass overrides the Painting value and sets each Painting's value to $25,000.
    // Write a main() function that declares an array of 10 Painting objects.  Prompt the 
    // user to enter the title and artist for each of the 10 paintings.  Consider the
    // Painting to be a FamousPainting, if the artist is one of the following:  Degas,
    // Monet, Picasso or Rembrandt.  Display the 10 paintings.
    
    
    #include<iostream.h>
    #include<conio.h>
    #include<string.h>
    
    class Painting
    {
    	private:
    		char title[30],
    			 painter[20];
    		double value;
    	public:
    		void setData(char paintingName[], char paintingArtist[]);
    		void outputData();
    		
    };
    
    class FamousPainter:public Painting
    {
    private:
    	double price;
    	public:
    		void showData();
    };
    
    void Painting::setData(char paintingName[], char paintingArtist[])
    {
    	double price = 400.00;
    	strcpy(title, paintingName);
    	strcpy(painter, paintingArtist);
    }
    
    void Painting::outputData()
    {
    	cout<<" This painting is worth $400.00"<<endl;
    }
    
    void FamousPainter::showData()
    {
    	cout<<"This painting is worth $25,000.00"<<endl;
    }
    
    
    void main()
    {
    	char name[10][30];
    	char artist[10][20];
    	int x;
    	
    	Painting paint;
    	FamousPainter amount;
    
    	for(x=0; x<10; ++x)
    	{
    		cout<<"Enter title of painting: ";
    		cin>>name[x];
    		cout<<"Enter the artist of painting: ";
    		cin>>artist[x];
    	}
    	
    
    	for(x=0; x<10; ++x)
    	{
    		cout<<name[x]<<" "<<artist[x];
    		if(*artist == "Degas" ||*artist == "Monet" ||
    			*artist == "Picasso"||*artist == "Rembrandt")
    			amount.showData();
    		else
    			paint.outputData();
    	}
    	paint.setData(name[x], artist[x]);
    	getch();
    }
    p.s. Read the message on the code tags...hope this is okay

    Thanks!

    Judy

  2. #2
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    I just skimmed through it but shouldnt there be another : at

    Code:
    class FamousPainter:public Painting

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    8

    Class Inheritance

    The chapter we're working on is class inheritance and it states to use only one : when using inheritance. That part seems to work, it's the if statement:

    [code]
    if(*artist == "Degas" ||*artist == "Monet" ||
    *artist == "Picasso"||*artist == "Rembrandt")
    amount.showData();
    [\code]

    It won't recognize the artist names specified above.

  4. #4
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    I dunno about that, isnt there a strcmp function your supposed to use for comparing? Beyond that, an array of arrays of chars can probably be written in a less error-prone method as a vector of strings.

  5. #5
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Also, when using an array of arrays, if you are set on that, you have to subscript it once to determine which artist you want, or else you get the first artist only, I think.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    8

    Reply

    Thanks for the input but like I said...I'm a student, this is all new to me. I tried the strcmp but I get errors.

  7. #7
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    strcmp takes two strings and compares them and if they are equal it returns 0.
    for example:

    Code:
    if(strcmp("String1","String2")==0) { // if equal
    
    } else if(strcmp("String1","String2")!=0) { // if not equal
    
    }

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    u had several errors in your code i fixed em a bit


    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<string.h>
    
    class Painting
    {
    	private:
    		char title[30],
    			 painter[20];
    		double value;
    	public:
    		void setData(char paintingName[], char paintingArtist[]);
    		void outputData();
    		
    };
    
    class FamousPainter:public Painting
    {
    private:
    	double price;
    	public:
    		void showData();
    };
    
    void Painting::setData(char paintingName[], char paintingArtist[])
    {
    	double price = 400.00;
    	strcpy(title, paintingName);
    	strcpy(painter, paintingArtist);
    }
    
    void Painting::outputData()
    {
    	cout<<" This painting is worth $400.00"<<endl;
    }
    
    void FamousPainter::showData()
    {
    	cout<<"This painting is worth $25,000.00"<<endl;
    }
    
    
    void main()
    {
    	char name[10][30];
    	char artist[10][20];
    	int x;
    	
    	Painting paint;
    	FamousPainter amount;
    
    	for(x=0; x<10; ++x)
    	{
    		cout<<"Enter title of painting: ";
    		cin>>name[x];
    		cout<<"Enter the artist of painting: ";
    		cin>>artist[x];
    	}
    	
    
    	for(x=0; x<10; ++x)
    	{
    		cout<<name[x]<<" "<<artist[x];
    		if(*artist == "Degas" ||*artist == "Monet" ||
    			*artist == "Picasso"||*artist == "Rembrandt")
    			amount.showData();
    		else
    			paint.outputData();
    	}
    	paint.setData(name[x], artist[x]);
    	getch();
    }

    and why do u use void main()

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    8

    Void Main

    Thanks for help, but I don't see any difference in the code...did the changes not save perhaps? To answer your question about why use void main()? Only because that's what I've been taught to do and that's what the book examples are showing. I have much admiration for anyone who programs in c++... this is pretty tough stuff to learn!

  10. #10
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    use {}
    around your if's

  11. #11
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    oh i see now what u are trying to do

  12. #12
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Only because that's what I've been taught to do and that's what the book examples are showing.
    Well, you've been taught wrong, ask Prelude for a lecture on void main()

    Oh well, thats beside the point.

    Code:
    	for(x=0; x<10; ++x)
    	{
    		cout<<name[x]<<" "<<artist[x];
    		if(*artist == "Degas" ||*artist == "Monet" ||
    			*artist == "Picasso" ||*artist == "Rembrandt")
    			amount.showData();
    		else
    			paint.outputData();
    	}
    Shouldn't this be

    Code:
    	for(x=0; x<10; ++x)
    	{
    		cout<<name[x]<<" "<<artist[x];
    		if(!strcmp(artist[x], "Degas") || !strcmp(artist[x], "Monet") ||
    			!strcmp(artist[x], "Picasso") || !strcmp(artist[x], "Rembrandt"))
    			amount.showData();
    		else
    			paint.outputData();
    	}

  13. #13
    Registered User
    Join Date
    Apr 2002
    Posts
    8
    The code worked!!! Thanks so much to everyone who spent time on this. I see now where I went wrong. About the void main(), this is my 2nd 'c' class and my first 'c++' class but it is supposed to be an advanced class at the community college. Through both semesters, I have been instructed to use void main() -- I don't know any other way. It would be interesting to hear the lecture on why not to use it.

    Anyway...thanks again - I appreciate the help!!

  14. #14
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    i cant believe that your school have'nt tought u how to use the
    standard way of main

    use int main()

    but remember to return a value at the end

    here is a simple ex.

    #include<iostream.h>

    int main()
    {
    cout<<"use int main()";
    return 0;
    }

  15. #15
    Registered User
    Join Date
    Apr 2002
    Posts
    8
    That's interesting because one of my co-workers recently started her first C++ class at a different school and was asking me (imagine that) for help with an assignment. Her book did use int main() and her program did have a return statement at the end.

    I'm just an access db developer and a wanna-be (real) programmer but have decided that C++ is not my forte. I just began using SQL and VB at work for application development. I'm reading all I can get my hands on to get up to speed on that, and with the class I'm taking I feel like my brain is going to blow up! How do you folks learn all this stuff??

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. accessing array of structs values
    By WaterNut in forum C++ Programming
    Replies: 12
    Last Post: 07-08-2004, 09:47 PM
  2. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 12:58 PM
  3. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 09:21 AM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21