6 Replies - 420 Views - Last Post: 22 February 2012 - 01:38 PM Rate Topic: -----

#1 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Data Not Reaching the Switch Statement?

Posted 22 February 2012 - 12:17 AM

#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

class Poly
{
	private:
		int exp[50];
		int deg;
	public:
		Poly()
		{
			for(int i = 0; i < 50; i++) exp[i] = 0;
		}

		void set(int a, int B)/>
		{
			exp[b] = a;
			deg = degree();
		}

		int degree()
		{
			int d = 0;
			for(int i = 0; i < 50; i++)
				if(exp[i] != 0) d = i;
			return d;
		}

		void print()
		{
			for(int i = 0; i < 50; i++)
			{
				if(exp[i] != 0)
				{
					if(exp[i] > 0) cout <<  "+ " <<  exp[i] << "x^" << i << " ";
					else cout <<  exp[i] << "x^" << i << " ";
				}
			}
		}

		Poly add(Poly B)/>
		{
			Poly c;

			for(int i = 0; i <= deg; i++) c.exp[i] += exp[i];
			for(int i = 0; i <= b.deg; i++) c.exp[i] += b.exp[i];

			c.deg = c.degree();
			return c;
		}

		Poly sub(Poly B)/>
		{
			Poly c;

			for(int i = 0; i <= deg; i++) c.exp[i] += exp[i];
			for(int i = 0; i <= b.deg; i++) c.exp[i] -= b.exp[i];

			c.deg = c.degree();
			return c;
		}

		Poly mult(Poly B)/>
		{
			Poly c;

			for(int i = 0; i <= deg; i++)
				for(int j = 0; j <= b.deg; j++)
					c.exp[i+j] += (exp[i] * b.exp[j]);

			c.deg = c.degree();
			return c;
		}
};

//int main()
//{
//	Poly a, b;
//	a.set(1,0);
//	a.set(2,1);
//	a.set(3,8);
//	a.print();
//	cout << "\n";
//
//	b.set(1,0);
//	b.set(2,1);
//	b.set(3,2);
//	b.print();
//	cout << "\n";
//
//	cout << "Add: ";
//	a.add(B)/>.print();
//	
//	cout << "\nSub: ";
//	a.sub(B)/>.print();
//
//	cout << "\nMult: ";
//	a.mult(B)/>.print();
//
//	system("PAUSE");
//	return 0;
//}

int main()
{
	Poly polynomials[2];
	Poly a, b;
	polynomials[0] = a;
	polynomials[1] = b;

	cout << "Welcome. Please enter two polynomials to manipulate.\n";
	system("PAUSE");

	for(int i = 0; i < 2; i++)
	{
		system("CLS");
		string c;
		cout << "Entering for polynomial " << i+1 << "\n";

		do
		{
			int coef, exp;
			cout << "Enter coefficient, then exponent of polynomial: \n";
			cin >> coef >> exp;
			polynomials[i].set(coef, exp);
			cout << "Again? (Y/N)   ";
			cin >> c;
		}while(c == "Y" || c == "y");
	}

	Poly c;
	int ch;
	do
	{
		system("CLS");
		cout << "(1) Addition\n";
		cout << "(2) Subtraction\n";
		cout << "(3) Multiplication\n";
		cout << "(4) Assignment\n";
		cout << "(5) Exit\n";
		cout << "Choice =====>  ";
		cin >> ch;

		if(ch < 0 || ch > 5) continue;

		switch(ch)
		{
			case 1:
				c = a.add(B)/>;
				break;
			case 2:
				c = a.sub(B)/>;
				break;
			case 3:
				c = a.mult(B)/>;
				break;
			case 4:
				//add assignment
				break;
			case 5:
				system("CLS");
				cout << "Goodbye\n";
				system("EXIT");
				break;
		}

		cout << "Result: ";
		c.print();
		system("PAUSE");

	} while(ch != 5);

	return 0;
}



So I've been working on this code for about 2 hours now. It's supposed to hold polynomials and then manipulate them. I have a commented main function that demonstrates that all my code is working as it should (the manipulations at least). My problem lies with the main method I'm trying to use right now. Everything runs fine and I don't get any errors, except that it seems that the functions within the switch statement aren't being called. The data doesn't reach there for some reason. I can't figure it out. I've narrowed it down to this place in the function being, what I think, is the problem area. If I could get any help, I would very thankful.

Also, in regard to some of my latest posts, sorry for the curtness of some of those, and my tendency to come and go only every few days, and not answer immediately. I've been busy, so I'm sorry for that.

Is This A Good Question/Topic? 0
  • +

Replies To: Data Not Reaching the Switch Statement?

#2 nuclearfroggy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 82
  • View blog
  • Posts: 167
  • Joined: 04-August 08

Re: Data Not Reaching the Switch Statement?

Posted 22 February 2012 - 01:36 AM

I think you mean to write
case '1': 
case '2':
case '3':

etc.
In ASCII the actual values 1, 2, 3... up to like 32 are control codes and basically not what you're expecting in the switch statement. The ASCII value for character '1' is actually 49. Also, you'll have to modify the if statement but I'm sure you get the picture :)

EDIT: Ah sorry, I should have looked closer, I didn't realise you're using an int type rather than a char

This post has been edited by nuclearfroggy: 22 February 2012 - 09:52 AM

Was This Post Helpful? 0
  • +
  • -

#3 sunilchintu2468  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 37
  • Joined: 29-May 11

Re: Data Not Reaching the Switch Statement?

Posted 22 February 2012 - 03:47 AM

View Postzim1985, on 22 February 2012 - 12:17 AM, said:

#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

class Poly
{
	private:
		int exp[50];
		int deg;
	public:
		Poly()
		{
			for(int i = 0; i < 50; i++) exp[i] = 0;
		}

		void set(int a, int B)/>
		{
			exp[b] = a;
			deg = degree();
		}

		int degree()
		{
			int d = 0;
			for(int i = 0; i < 50; i++)
				if(exp[i] != 0) d = i;
			return d;
		}

		void print()
		{
			for(int i = 0; i < 50; i++)
			{
				if(exp[i] != 0)
				{
					if(exp[i] > 0) cout <<  "+ " <<  exp[i] << "x^" << i << " ";
					else cout <<  exp[i] << "x^" << i << " ";
				}
			}
		}

		Poly add(Poly B)/>
		{
			Poly c;

			for(int i = 0; i <= deg; i++) c.exp[i] += exp[i];
			for(int i = 0; i <= b.deg; i++) c.exp[i] += b.exp[i];

			c.deg = c.degree();
			return c;
		}

		Poly sub(Poly B)/>
		{
			Poly c;

			for(int i = 0; i <= deg; i++) c.exp[i] += exp[i];
			for(int i = 0; i <= b.deg; i++) c.exp[i] -= b.exp[i];

			c.deg = c.degree();
			return c;
		}

		Poly mult(Poly B)/>
		{
			Poly c;

			for(int i = 0; i <= deg; i++)
				for(int j = 0; j <= b.deg; j++)
					c.exp[i+j] += (exp[i] * b.exp[j]);

			c.deg = c.degree();
			return c;
		}
};

//int main()
//{
//	Poly a, b;
//	a.set(1,0);
//	a.set(2,1);
//	a.set(3,8);
//	a.print();
//	cout << "\n";
//
//	b.set(1,0);
//	b.set(2,1);
//	b.set(3,2);
//	b.print();
//	cout << "\n";
//
//	cout << "Add: ";
//	a.add(B)/>.print();
//	
//	cout << "\nSub: ";
//	a.sub(B)/>.print();
//
//	cout << "\nMult: ";
//	a.mult(B)/>.print();
//
//	system("PAUSE");
//	return 0;
//}

int main()
{
	Poly polynomials[2];
	Poly a, b;
	polynomials[0] = a;
	polynomials[1] = b;

	cout << "Welcome. Please enter two polynomials to manipulate.\n";
	system("PAUSE");

	for(int i = 0; i < 2; i++)
	{
		system("CLS");
		string c;
		cout << "Entering for polynomial " << i+1 << "\n";

		do
		{
			int coef, exp;
			cout << "Enter coefficient, then exponent of polynomial: \n";
			cin >> coef >> exp;
			polynomials[i].set(coef, exp);
			cout << "Again? (Y/N)   ";
			cin >> c;
		}while(c == "Y" || c == "y");
	}

	Poly c;
	int ch;
	do
	{
		system("CLS");
		cout << "(1) Addition\n";
		cout << "(2) Subtraction\n";
		cout << "(3) Multiplication\n";
		cout << "(4) Assignment\n";
		cout << "(5) Exit\n";
		cout << "Choice =====>  ";
		cin >> ch;

		if(ch < 0 || ch > 5) continue;

		switch(ch)
		{
			case 1:
				c = a.add(B)/>;
				break;
			case 2:
				c = a.sub(B)/>;
				break;
			case 3:
				c = a.mult(B)/>;
				break;
			case 4:
				//add assignment
				break;
			case 5:
				system("CLS");
				cout << "Goodbye\n";
				system("EXIT");
				break;
		}

		cout << "Result: ";
		c.print();
		system("PAUSE");

	} while(ch != 5);

	return 0;
}



So I've been working on this code for about 2 hours now. It's supposed to hold polynomials and then manipulate them. I have a commented main function that demonstrates that all my code is working as it should (the manipulations at least). My problem lies with the main method I'm trying to use right now. Everything runs fine and I don't get any errors, except that it seems that the functions within the switch statement aren't being called. The data doesn't reach there for some reason. I can't figure it out. I've narrowed it down to this place in the function being, what I think, is the problem area. If I could get any help, I would very thankful.

Also, in regard to some of my latest posts, sorry for the curtness of some of those, and my tendency to come and go only every few days, and not answer immediately. I've been busy, so I'm sorry for that.


Functions are being called but it's giving segmentation fault. I think you are not using the memory properly check it out why it is failing
Output which I got :
Welcome. Please enter two polynomials to manipulate.
sh: PAUSE: command not found
sh: CLS: command not found
Entering for polynomial 1
Enter coefficient, then exponent of polynomial:
1
1
Again? (Y/N) n
sh: CLS: command not found
Entering for polynomial 2
Enter coefficient, then exponent of polynomial:
1
1
Again? (Y/N) n
sh: CLS: command not found
(1) Addition
(2) Subtraction
(3) Multiplication
(4) Assignment
(5) Exit
Choice =====> 1
Segmentation fault
Was This Post Helpful? 0
  • +
  • -

#4 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Data Not Reaching the Switch Statement?

Posted 22 February 2012 - 08:53 AM

View Postnuclearfroggy, on 22 February 2012 - 12:36 AM, said:

I think you mean to write
case '1': 
case '2':
case '3':

etc.
In ASCII the actual values 1, 2, 3... up to like 32 are control codes and basically not what you're expecting in the switch statement. The ASCII value for character '1' is actually 49. Also, you'll have to modify the if statement but I'm sure you get the picture :)

That's weird. I've never had this issue in other programs before...I'm running in an in to the switch, not a char, in which case I'd be completely on board with this explanation.

And then there was the issue of memory segmentation or something like that? I've never even heard of something like that to remotely know how to fix it. If I could get an explanation so I could fix it, not to have you guys do the work for me.
Was This Post Helpful? 0
  • +
  • -

#5 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: Data Not Reaching the Switch Statement?

Posted 22 February 2012 - 09:09 AM

The error is having four Poly instances when you should only have two.
line 111
polynomials[i] = a; 
copies the values of a into polynomials[i] and does not pass a reference. You then call
polynomials[i].set( coef, exp)
in your loop. You then call
a.add(b )
in switch. You should be calling
polynomials[0].add(polynomials[1] )
instead.

The value of a.reg is garbage so when you use it to control array access you get a segfault as you're accessing data not in the array.

EDIT: should be polynomials[1] and not b.

This post has been edited by simeesta: 22 February 2012 - 09:32 AM

Was This Post Helpful? 2
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4060
  • View blog
  • Posts: 12,540
  • Joined: 25-December 09

Re: Data Not Reaching the Switch Statement?

Posted 22 February 2012 - 09:19 AM

Have you tried running this program through your debugger? It appears to me that the functions in the case statement are indeed being called, using the code in your original post.

Please post an example of your input, what your program is outputting with said input, and what you expect your program to output.

Edit: Please stop using all the system() calls. They are non-standard and it is considered a bad practice to use system() calls to pause program execution, or clear the screen.

Jim

This post has been edited by jimblumberg: 22 February 2012 - 09:21 AM

Was This Post Helpful? 0
  • +
  • -

#7 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Data Not Reaching the Switch Statement?

Posted 22 February 2012 - 01:38 PM

View Postsimeesta, on 22 February 2012 - 08:09 AM, said:

The error is having four Poly instances when you should only have two.
line 111
polynomials[i] = a; 
copies the values of a into polynomials[i] and does not pass a reference. You then call
polynomials[i].set( coef, exp)
in your loop. You then call
a.add(b )
in switch. You should be calling
polynomials[0].add(polynomials[1] )
instead.

The value of a.reg is garbage so when you use it to control array access you get a segfault as you're accessing data not in the array.

EDIT: should be polynomials[1] and not b.

This worked perfectly. I should have caught that. Thank you. I'll be sure to be keeping this in mind for future reference.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1