Thread: if error

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    34

    if error

    if (choice = "add")

    whats wrong with this line
    choice is a string variable

    i get this error


    --------------------Configuration: lessons - Win32 Debug--------------------
    Compiling...
    test1.cpp
    C:\Program Files\Microsoft Visual Studio\MyProjects\lessons\test1.cpp(20) : error C2451: conditional expression of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' is illegal
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    Error executing cl.exe.

    test1.obj - 1 error(s), 0 warning(s)

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    34
    never mind just had to do if(choice =="add")
    instead of if(choice ="add")

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    what variable is choice?

    assuming you're using string classes:

    1) are you intending to assign "add" to choice?
    2) i'm not sure a string class can evaluate to a boolean expression.

  4. #4
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    char * choice = "add";
    if ( choice == "add" )
    The world is waiting. I must leave you now.

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Shadow
    char * choice = "add";
    if ( choice == "add" )
    if ( strcmp(choice, "add") == 0 )

  6. #6
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Yes, string compare, is useful.

    But, as the OP first stated his problem:
    > if (choice = "add")
    (which was later corrected to choice == "add")
    I was confused to all heck on just what he was trying to do.
    The world is waiting. I must leave you now.

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    72
    this:

    if ( answer1 == real1)

    correct1 == 1;
    else
    correct1 == 0;

    where answer1 is inputted using "cin" and real1 is set by:

    strcpy(real1,"clrscr();");

    how do i fix it

    i really need help badly for school

    can someone tell me EXACTLY what to do to fix this?

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>how do i fix it
    Fix what exactly? Can you explain your question, I don't understand what you want.

    Does this help:
    Code:
    if (strcmp(answer1, real1) == 0)
        cout <<"Same" <<endl;
    else    
        cout <<"Different" <<endl;
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    72
    Code:
    //Program Name: ngquiz.cpp//
    //Revision Date: 2002-09-08//
    //Explanation: using variables and strings, create a quizzer program that will display a remedial quizzer similar to the one used school//
    
    #include<iostream.h>
    #include<conio.h>
    #include<time.h>
    #include<string.h>
    #include<math.h>
    
    char copy1[80],copy2[80], copy3[80] ,last[80], first[80], teacher[80], question1[80],  question2[80], answer2[80], real2[80], question3[80], answer3[80], real3[80], check1[10], check2[10], check3[10],answer1[80], real1[80];
    int  count, grade, goal, correct1, correct2, correct3, totalcorrect, average;
    
    main()
    
    {
    
    
    textattr(18);
    
    count = 0;
    
    time_t t=time(NULL);
    
    
    strcpy(question1,"What is the total line to clear the screen?");
    strcpy(real1,"clrscr();");
    strcpy(question2,"What library is needed to use the command to clear the screen?");
    strcpy(real2,"conio.h");
    strcpy(question3,"What command is used to display characters on the screen?");
    strcpy(real3,"cout");
    
    	cout<<"			Quizzer Program for C++"<<endl;
    	cout<<"			~~~~~~~~~~~~~~~~~~~~~~~"<<endl<<endl;
    	cout<<"What is your surname?: ";
    	cin>>last;
    	cout<<"What is your given name?: ";
    	cin>>first;
    	cout<<"What is your teacher's name?: ";
    	cin>>teacher;
    	cout<<"What grade are you in?: ";
    	cin>>grade;
    	cout<<"What is your goal?(0-3): ";
    	cin>>goal;
    	cout<<endl;
    	cout<<"Press Spacebar to Continue..."<<endl;
    	getch();
    
    
    	do
    
    	{
    
    		count= count +1;
    
    		if (count == 1)
    		{
    			clrscr();
    
    			cout<<ctime(&t)<<endl<<endl;
    			cout<<"                 C++ Quizzer"<<endl;
    			cout<<"			         ~~~~~~~~~~~"<<endl<<endl;
    			cout<<"Question 1: "<<endl;
    			cout<<"	 "<<question1<<endl<<endl;
    			cout<<endl;
    			cout<<endl;
    			cin>>answer1;
             cout<<endl;
            	cout<<"Press The Spacebar To Continue..."<<endl;
    			getch();
    
    			clrscr();
    
    			cout<<ctime(&t)<<endl<<endl;
    			cout<<"			         C++ Quizzer"<<endl;
    			cout<<"			         ~~~~~~~~~~~"<<endl<<endl;
    			cout<<"Your answer: "<<answer1<<endl;
             cout<<"The Correct Answer: "<<real1<<endl;
    
             getch();
    
    		}
    
    		if (count == 2)
    		{
    			clrscr();
    
    			cout<<ctime(&t)<<endl<<endl;
    			cout<<"                 C++ Quizzer"<<endl;
    			cout<<"			         ~~~~~~~~~~~"<<endl<<endl;
    			cout<<"Question 2: "<<endl;
    			cout<<question2<<endl<<endl;
    			cout<<endl;
    			cout<<endl;
    			cin>>answer2;
    			cout<<"Press The Spacebar To Continue..."<<endl;
    			getch();
    
    			clrscr();
    
    			cout<<ctime(&t)<<endl<<endl;
    			cout<<"			         C++ Quizzer"<<endl;
    			cout<<"			         ~~~~~~~~~~~"<<endl<<endl;
    			cout<<"Your answer: "<<answer2<<endl;
             cout<<"The Correct Answer: "<<real2<<endl;
    
             getch();
    
    		}
    
    		if (count == 3)
    
    		{
    			clrscr();
    
    			cout<<ctime(&t)<<endl<<endl;
    			cout<<"                 C++ Quizzer"<<endl;
    			cout<<"			         ~~~~~~~~~~~"<<endl<<endl;
    			cout<<"Question 3: "<<endl;
    			cout<<"	 "<<question3<<endl<<endl;
    			cout<<endl;
    			cout<<endl;
    			cin>>answer3;
    			cout<<"Press The Spacebar To Continue..."<<endl;
    			getch();
    
    			clrscr();
    
    			cout<<ctime(&t)<<endl<<endl;
    			cout<<"			         C++ Quizzer"<<endl;
    			cout<<"			         ~~~~~~~~~~~"<<endl<<endl;
    			cout<<"Your answer: "<<answer3<<endl;
             cout<<"The Correct Answer: "<<real3<<endl;
    
             getch();
             }
    
    
    
    
    	}
    
    	while (count<=3);
    
    
       cout<<answer1<<endl;
       cout<<answer2<<endl;
       cout<<answer3<<endl;
    
       getch();
    	clrscr();
    
    
       if ( answer1 == real1)
    
                correct1 == 1;
    	else
                correct1 == 0;
    
    
       if (answer2 == real2)
    				{
                correct2=1;
                }
    	else
    				{
                correct2=0;
                }
    
       if (answer3==real3)
    				{
                correct3=1;
                }
       else
    				{
                correct3=0;
                }
    
    
    
    
    
    
    	cout<<"								"<<ctime(&t)<<endl;
    	cout<<"			    C++ Quizzer Summary"<<endl;
    	cout<<"			    ~~~~~~~~~~~~~~~~~~~"<<endl<<endl;
    	cout<<"Summary For: "<<last<<", "<<first<<endl;
    	cout<<"Teacher: "<<teacher<<endl;
    	cout<<"Grade: "<<grade<<endl;
    	cout<<"Goal: "<<goal<<endl;
    	cout<<endl;
    	cout<<"Question 1-					  Your Answer:	"<<answer1<<endl;
    	cout<<endl;
    	cout<<"Question 2-					  Your Answer:	"<<answer2<<endl;
    	cout<<endl;
    	cout<<"Question 3-					  Your Answer:	"<<answer3<<endl;
    	cout<<endl;
    
    totalcorrect= correct1 + correct2 + correct3;
    
    	cout<<"Questions Answered Correctly:  "<<totalcorrect<<endl;
    
    		if (totalcorrect==0)
    		{
    
    			average=(0/3)*100;
    		}
    
    
    		if (totalcorrect==1)
    		{
    
    			average=(1/3)*100;
    		}
    
    
    		if (totalcorrect==2)
    		{
    
    			average=(2/3)*100;
    		}
    
    
    		if (totalcorrect==3)
    		{
    
    			average=(3/3)*100;
    		}
    
    	cout<<endl;
    	cout<<"Your Average is: "<<average<<"%"<<endl;
    
    
    	getch();
    
    	clrscr();
    
    	cout<<endl;
    	cout<<endl;
    	cout<<endl;
    	cout<<endl;
    	cout<<endl;
    	cout<<endl;
    	cout<<endl;
    	cout<<endl;
    	cout<<endl;
    	cout<<endl;
    	cout<<endl;
    	cout<<endl;
    	cout<<endl;
    	cout<<endl;
    	cout<<endl;
    	cout<<"			    Happy Test Typing!!!"<<endl;
    	cout<<endl;
    	cout<<endl;
    	cout<<endl;
    	cout<<endl;
    	cout<<endl;
    	cout<<endl;
    	cout<<endl;
    	cout<<endl;
    	cout<<endl;
    	cout<<endl;
    	cout<<endl;
    	cout<<endl;
    	cout<<endl;
    	cout<<endl;
    	cout<<endl;
    	cout<<endl;
    
    }
    that is my whole program, i just need some help with the loops
    Last edited by niroopan; 09-15-2002 at 08:43 AM.

  10. #10
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    YES, CODE TAGS.

    Man alive...
    Why all the endl's?
    Just throw em in a loop or something.

    That code is a mess.
    The world is waiting. I must leave you now.

  11. #11
    Registered User
    Join Date
    Sep 2002
    Posts
    72
    hey, this is my 2nd DAY of C++, i think it pretty good for my 2nd day, all i need is a little or a lot of help and guidance from you experience guys. thats all i want

  12. #12
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > all i need is a little or a lot of help
    Ok, edit your post.
    Delete the all of the pasted code.
    Use code tags.
    Copy the code from your text editor inbetween the code tags.

    Explain problems in the future with details, and explanations of what it is you are looking to achieve.
    The world is waiting. I must leave you now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM