13 Replies - 5855 Views - Last Post: 30 June 2009 - 12:21 PM Rate Topic: -----

#1 Pipey85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 30-June 09

error c2446 and error c2040 (beginner)

Post icon  Posted 30 June 2009 - 11:05 AM

OK, ive only been doing c++ programming for about 2 weeks, after watching many tutorial videos, and this is my 1st attempt at defining/declaring functions, and using the switch statement, but i keep getting these two error messages.....


error C2446: '==' : no conversion from 'const char *' to 'int'
1> There is no context in which this conversion is possible

and
error C2040: '==' : 'int' differs in levels of indirection from 'const char [4]'

they both refer to the if statement that is pointed out.


EDIT: i have no idea how well the program will actually work so please dont spoil it for me :)






#include "stdafx.h"
#include <iostream>
using namespace std;

int multiply (int num1, int num2);

int divide (int num1, int num2);

int subtract (int num1, int num2);

int add(int num1, int num2);


int main()
{
	bool start = true;
	int num1;
	int num2;
	char strt;
	int op;

	while (start == true)
	cout << "System ready\n" << "Please give 1st of two numbers\n";
	cin >> num1;
	cout << "Please give 2nd number\n";
	cin >> num2;
	cout << "What would you like to do with these numbers?\n1.Add\n2.subtract\n3.multiply\n4.divide";
	cin >> op;

	switch (op)
	{
		case 1:
			{
			add (num1, num2);
			break;
			}
		case 2:
			{
			subtract (num1, num2);
			break;
			}
		case 3:
			{
			multiply (num1, num2);
			break;
			}
		case 4:
			{
			divide (num1, num2);
			break;
			}
		default:
	{
			cout << "\nPROGRAM ERROR!!!";
				start = true;
	}
	}
		
	
	
			cout << "\nWould you like to start again?";
		cin >> strt;

		
		if (strt == 'y' || strt == 'Y' || strt == "yes") //This line here is what seems to be giving me 
			start = true;							  //				  trouble
		else
			start = false;

	return 0;
}





int multiply (int num1, int num2)
{
	int result;
	result = num1 * num2;
	return result;
}
int divide (int num1, int num2)
{
	int result;
	result = num1 / num2;
	return result;
}
int subtract (int num1, int num2)
{
	int result;
	result = num1 - num2;
	return result;
}
int add(int no1, int no2)

{
	int rsult = no1 + no2;
	return rsult;
}



please note, i know there are 'cleaner' ways to make a simple calculator, but this is to try and understand the switch statement and the calling functions, thanx guys.

This post has been edited by Pipey85: 30 June 2009 - 11:07 AM


Is This A Good Question/Topic? 0
  • +

Replies To: error c2446 and error c2040 (beginner)

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: error c2446 and error c2040 (beginner)

Posted 30 June 2009 - 11:17 AM

you are trying to compare a char to a string
[quote]
char strt;

		if (strt == 'y' || strt == 'Y' || strt == "yes") //This line here is what seems to be giving me 
			start = true;							  //				  trouble
		else
			start = false;
[/quote]



 char
can only hold one character like 'Y';

you cant compare a char( ' ') to a string(" "), its illegal.
either change the string comparison to a char or do this;
 
#include <string>
//-------------------

string strt;

cin>>strt;
 if (strt == "y" || strt == "Y" || strt == "yes") 
{

}


Was This Post Helpful? 0
  • +
  • -

#3 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: error c2446 and error c2040 (beginner)

Posted 30 June 2009 - 11:19 AM

if (strt == 'y' || strt == 'Y' || strt == "yes") //This



1-chars you use " ' "
2-yes is not a char it's an array of chars or a string. don't do that

besides that it should work fine. hope that helps :)

PS damn you sexy lol same time...

This post has been edited by computerfox: 30 June 2009 - 11:20 AM

Was This Post Helpful? 1
  • +
  • -

#4 Pipey85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 30-June 09

Re: error c2446 and error c2040 (beginner)

Posted 30 June 2009 - 11:20 AM

thankyou, ive got to say the thing i love most about all this programming is that it is the most smallest of things that make such a hughe difference :P
Was This Post Helpful? 0
  • +
  • -

#5 red_4900  Icon User is offline

  • Code T(h)inkers
  • member icon

Reputation: 21
  • View blog
  • Posts: 1,119
  • Joined: 22-February 08

Re: error c2446 and error c2040 (beginner)

Posted 30 June 2009 - 11:21 AM

Here's the problem :
if (strt == 'y' || strt == 'Y' || strt == "yes"

strt variable is of type char, so it can only hold one char. You're asking the program if strt is "yes", which is of string type.

....I lose. Why did you guys reply so fast? Zzzz

This post has been edited by red_4900: 30 June 2009 - 11:22 AM

Was This Post Helpful? 0
  • +
  • -

#6 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: error c2446 and error c2040 (beginner)

Posted 30 June 2009 - 11:24 AM

View PostPipey85, on 30 Jun, 2009 - 10:20 AM, said:

thankyou, ive got to say the thing i love most about all this programming is that it is the most smallest of things that make such a hughe difference :P



indeed. to tell you the truth the small stuff still happens to us too, well except pbl and martyr <3
Was This Post Helpful? 0
  • +
  • -

#7 Mowgef  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 245
  • Joined: 01-May 09

Re: error c2446 and error c2040 (beginner)

Posted 30 June 2009 - 11:27 AM

I smash my face every time i get a seg fault
Was This Post Helpful? 0
  • +
  • -

#8 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: error c2446 and error c2040 (beginner)

Posted 30 June 2009 - 11:40 AM

really? i just get really pissed off at myself and start yelling at myself.
Was This Post Helpful? 0
  • +
  • -

#9 Mowgef  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 245
  • Joined: 01-May 09

Re: error c2446 and error c2040 (beginner)

Posted 30 June 2009 - 11:46 AM

yeah, words don't work anymore.

I hate taking a break from using C/C++ and try to use like scanf, and I'll forget the &. Its been a while since thats happened but I hate it so much. GRRR
Was This Post Helpful? 0
  • +
  • -

#10 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: error c2446 and error c2040 (beginner)

Posted 30 June 2009 - 11:47 AM

i actually prefer cout, but isn't c-echo?
Was This Post Helpful? 0
  • +
  • -

#11 Mowgef  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 245
  • Joined: 01-May 09

Re: error c2446 and error c2040 (beginner)

Posted 30 June 2009 - 11:50 AM

yeah I switch off between cout, printf, etc.

php is echo.
Was This Post Helpful? 0
  • +
  • -

#12 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: error c2446 and error c2040 (beginner)

Posted 30 June 2009 - 12:12 PM

well, i knew PHP was echo, but i also thought C.
Was This Post Helpful? 0
  • +
  • -

#13 Mowgef  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 245
  • Joined: 01-May 09

Re: error c2446 and error c2040 (beginner)

Posted 30 June 2009 - 12:18 PM

I don't think so...
Was This Post Helpful? 0
  • +
  • -

#14 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: error c2446 and error c2040 (beginner)

Posted 30 June 2009 - 12:21 PM

oh okay. just thought. i don't really know C, but i know it's a lot like C++ and as long as you understand the concept of programming the syntax should be easy to learn.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1