overloaded funtion with sqrt c++(helped)

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
vegiestirfrypizza
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 5
Joined: Wed Nov 30, 2011 12:27 pm
Current Project: learning basics of c++
Favorite Gaming Platforms: famicom
Programming Language of Choice: C++
Location: iowa city

overloaded funtion with sqrt c++(helped)

Post by vegiestirfrypizza »

the error reads as "more than one instance of overloaded funtion sqrt in argument list" is there something wrong with #include <cmath>?

Code: Select all

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

int main()
{
cout<<"please enter a value to be evaluated as prime or not prime";
int x=0;
cin>>x;
int loop=2;
int prime=true;
while(loop == sqrt(x));<----this is the error.
{
if (x % loop==0)
	prime=false;
loop++;

}

if(prime)
	cout<<"number is prime";
else
	cout<<"number is not prime";



	system("PAUSE");
	return 0;
}

thank you for you're time.
error is in sqrt()
Last edited by vegiestirfrypizza on Sun Mar 25, 2012 1:21 am, edited 1 time in total.
the morning is good for algorithms and tough thinking, the night is for things that don't require such forethought.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: overloaded funtion with sqrt c++

Post by dandymcgee »

First off the while([condition]) line should not end with a semi-colon. Second, sqrt() returns a double or a float depending on what type you pass as a parameter.

To get your code to compile you could simply cast the return value to an int and remove the semi-colon:

Code: Select all

while(loop == (int)sqrt(x))
Also, I'm not sure how you came up with this code but it's definitely not going to properly determine whether or not x is prime.

Here is an example prime test for your reference:

Code: Select all

#include <cmath>
#include <iostream>
using namespace std;

int main()
{
	int num;
	cin >> num;
	
	if (num <=1)
		return false;
	else if (num == 2)		 
		return true;
	else if (num % 2 == 0)
		return false;
	else
	{
		bool prime = true;
		int divisor = 3;
		double num_d = static_cast<double>(num);
		int upperLimit = static_cast<int>(sqrt(num_d) +1);
		
		while (divisor <= upperLimit)
		{
			if (num % divisor == 0)
				prime = false;
			divisor +=2;
		}
		cout << num << (prime) ? " is prime." : " is not prime.";
	}
	
	cin.get();
}
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
vegiestirfrypizza
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 5
Joined: Wed Nov 30, 2011 12:27 pm
Current Project: learning basics of c++
Favorite Gaming Platforms: famicom
Programming Language of Choice: C++
Location: iowa city

Re: overloaded funtion with sqrt c++

Post by vegiestirfrypizza »

thanks, i actually got it from a c++ book, maybe its not the best resource...not the assignment every line of code you see is in fact, not actually mine although it would be just as bad. but yeah, thank you for a better prime number code, and thanks again. :worship:
the morning is good for algorithms and tough thinking, the night is for things that don't require such forethought.
Post Reply