473,394 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Please help

1
I can't figure out the problem with this code. I'm using a Borland compiler. The problem is with the / operation overload. When I call isNeg it doesn't output true or false. Now I know this code isn't pretty but our teacher doesn't teach us syntax or anything so I'm doing the best I can.

Expand|Select|Wrap|Line Numbers
  1. class Juggernaut
  2. {
  3. public: 
  4.     friend Juggernaut operator +(Juggernaut a, Juggernaut b);
  5.     friend Juggernaut operator -(Juggernaut a, Juggernaut b);
  6.     friend Juggernaut operator *(Juggernaut a, Juggernaut b);
  7.     friend long int operator /(Juggernaut a, Juggernaut b);
  8.  
  9.     Juggernaut(string a);
  10.     Juggernaut();
  11.     void output();
  12.     int size();
  13.     void reverse();
  14.     bool Negative();
  15.     void setNegative();
  16.     void flip();
  17. private:
  18.     vector<int> Jugg;
  19.     bool isNeg;
  20. };
  21.  
  22. Juggernaut::Juggernaut()
  23. {}
  24.  
  25. Juggernaut::Juggernaut(string temp)
  26. {
  27.     int hold = temp[0];
  28.     if(hold != 45) //checks ASCII to see if there is a negative sign in the front
  29.     {
  30.         isNeg = false;
  31.         for(int i = 0; i < temp.length(); i++)
  32.         {
  33.             Jugg.push_back(temp[i] - 48); // the -48 converts ASCII to int
  34.         }
  35.         cout << endl;
  36.     }
  37.     else
  38.     {
  39.         isNeg = true;
  40.         for(int j = 1; j < temp.length(); j++)
  41.         {
  42.             Jugg.push_back(temp[j] - 48);
  43.         }
  44.         cout << endl;
  45.     }
  46. }
  47.  
  48. void Juggernaut::flip()
  49. {
  50.     Juggernaut temp;
  51.     for(int i=Jugg.size()-1; i>=0; i--)
  52.     {
  53.         temp.Jugg.push_back(Jugg[i]);
  54.     }
  55.     for(int j=0; j<Jugg.size(); j++)
  56.     {
  57.         Jugg[j] = temp.Jugg[j];
  58.     }
  59. }
  60.  
  61. void Juggernaut::reverse()
  62. {
  63.     if(isNeg == true)
  64.         cout << "-";
  65.     for(int i=Jugg.size()-1; i>=0; i--)
  66.         cout << Jugg[i];
  67. }
  68.  
  69. int Juggernaut::size()
  70. {
  71.     return (Jugg.size());
  72. }
  73.  
  74. //Precondition: isNeg is set
  75. //Postcondition: returns isNeg to non friendly functions use
  76. bool Juggernaut::Negative()
  77. {
  78.     return isNeg;
  79. }
  80.  
  81. void Juggernaut::setNegative()
  82. {
  83.     isNeg = true;
  84. }
  85.  
  86. void Juggernaut::output()
  87. {
  88.     if(isNeg == true)
  89.         cout << "-";
  90.     for(int i=0; i<Jugg.size(); i++)
  91.         cout << Jugg[i];
  92. }
  93.  
  94. class menu 
  95. {
  96. public:
  97.     menu();
  98.     void output();
  99.     void implement();
  100.     void getNums();
  101.  
  102. private:
  103.     int choice;
  104.     Juggernaut a;
  105.     Juggernaut b;
  106.     Juggernaut c;
  107.  
  108. };
  109.  
  110. menu::menu()
  111. {
  112.     choice = 0;
  113. }
  114.  
  115. void menu::getNums()
  116. {
  117.     string temp, temp2;
  118.     cout << "\nEnter the first number: ";
  119.     cin >> temp;
  120.     a = Juggernaut(temp);
  121.     cout << "Enter the second number: ";
  122.     cin >> temp2;
  123.     b = Juggernaut(temp2);
  124. }
  125.  
  126. void menu::output()
  127. {
  128.     cout << "Welcome to the JuggernautInteger Calculator!";
  129.     while(choice !=5)
  130.     {
  131.         cout << "\n\n1. addition\n2. subtraction\n3. multiplication\n4. division\n5. exit\n\n"
  132.               << "Please make a selection: ";
  133.         cin >> choice;
  134.         implement();
  135.     }
  136.     cout << "\nSee Ya!";
  137. }
  138.  
  139. Juggernaut operator +(Juggernaut a, Juggernaut b)
  140. {
  141.     Juggernaut c;
  142.     int carry = 0;//holds the carry over from one int to the next
  143.     if(a.size() == b.size())//This is to check numbers of the same length
  144.     {
  145.         for(int i = (a.size()-1); i>=0 ; i--)
  146.         {
  147.             if((a.Jugg[i] + b.Jugg[i] + carry)<10) // this is if there is no carry needed
  148.             {
  149.                 c.Jugg.push_back(a.Jugg[i]+b.Jugg[i] + carry);
  150.                 carry = 0;
  151.             }
  152.             else
  153.             {
  154.                 c.Jugg.push_back((a.Jugg[i]+b.Jugg[i] + carry)-10);
  155.                 carry = 1;
  156.             }
  157.         }
  158.         if(carry == 1)//outputs carry if the is nothing for it to be added to.
  159.             c.Jugg.push_back(carry);
  160.     }
  161.     else if(a.size() > b.size())//checks to see if a is longer the b
  162.     {
  163.         int j = a.size()-1;
  164.         for(int i = b.size()-1; i>=0; i--)
  165.         {
  166.             if((a.Jugg[j] + b.Jugg[i] + carry)<10)
  167.             {
  168.                 c.Jugg.push_back(a.Jugg[j]+b.Jugg[i] + carry);
  169.                 carry = 0;
  170.             }
  171.             else
  172.             {
  173.                 c.Jugg.push_back((a.Jugg[j]+b.Jugg[i] + carry)-10);
  174.                 carry = 1;
  175.             }
  176.             j--;        
  177.         }
  178.         while(j>=0)
  179.         {
  180.             c.Jugg.push_back(a.Jugg[j]+carry);
  181.             carry = 0;
  182.             j--;
  183.         }
  184.     }
  185.     else
  186.     {
  187.         int j = b.size()-1;
  188.         for(int i = a.size()-1; i>=0; i--)
  189.         {
  190.             if((a.Jugg[i] + b.Jugg[j] + carry)<10)
  191.             {
  192.                 c.Jugg.push_back(a.Jugg[i]+b.Jugg[j] + carry);
  193.                 carry = 0;
  194.             }
  195.             else
  196.             {
  197.                 c.Jugg.push_back((a.Jugg[i]+b.Jugg[j] + carry)-10);
  198.                 carry = 1;
  199.             }
  200.             j--;        
  201.         }
  202.         while(j>=0)
  203.         {
  204.             c.Jugg.push_back(b.Jugg[j]+carry);
  205.             carry = 0;
  206.             j--;
  207.         }
  208.     }
  209.     return c;
  210. }
  211.  
  212. Juggernaut operator -(Juggernaut a, Juggernaut b)
  213. {
  214.     Juggernaut c;
  215.     char smaller = 'Z';
  216.     int i = 0;
  217.     if(a.size() == b.size())//When they are the same size this bit figures out which is bigger
  218.     {                       // because it makes a difference in the sign given
  219.         while(smaller == 'Z' && i < a.size())
  220.         {
  221.             if(a.Jugg[i] > b.Jugg[i])
  222.                 smaller = 'B';
  223.             else if(a.Jugg[i] < b.Jugg[i])
  224.                 smaller = 'A';
  225.             else
  226.                 i++;
  227.         }
  228.         if(smaller == 'Z')
  229.             c.Jugg.push_back(0);
  230.     }
  231.     if(a.size()>b.size() || smaller == 'B')
  232.     {
  233.         int j = a.size()-1;
  234.         for(int i = b.size()-1; i >=0; i--)
  235.         {
  236.             if(a.Jugg[j] >= b.Jugg[i])
  237.                 c.Jugg.push_back(a.Jugg[j]-b.Jugg[i]);
  238.             else
  239.             {
  240.                 bool cont = true;
  241.                 int k =1;
  242.                 while(cont == true)
  243.                 { 
  244.                     if(a.Jugg[j-k] != 0)
  245.                     {
  246.                         a.Jugg[j-k] = a.Jugg[j-k]-1;
  247.                         cont = false;
  248.                     }
  249.                     else
  250.                     {
  251.                         a.Jugg[j-k] = 9;
  252.                         k++;
  253.                     }
  254.                 }
  255.                 a.Jugg[j] = a.Jugg[j] + 10;
  256.                 c.Jugg.push_back(a.Jugg[j] - b.Jugg[i]);
  257.             }
  258.             j--;
  259.         }
  260.         while(j>=0)
  261.         {
  262.             c.Jugg.push_back(a.Jugg[j]);
  263.             j--;
  264.         }
  265.     }
  266.     else if(a.size()<b.size()|| smaller == 'A')
  267.     { 
  268.         c.isNeg = true;
  269.  
  270.         int j = b.size()-1;
  271.         for(int i = a.size()-1; i >=0; i--)
  272.         {
  273.             if(b.Jugg[j] >= a.Jugg[i])
  274.                 c.Jugg.push_back(b.Jugg[j]-a.Jugg[i]);
  275.             else
  276.             {
  277.                 bool cont = true;
  278.                 int k =1;
  279.                 while(cont == true)
  280.                 { 
  281.                     if(b.Jugg[j-k] != 0)
  282.                     {
  283.                         b.Jugg[j-k] = b.Jugg[j-k]-1;
  284.                         cont = false;
  285.                     }
  286.                     else
  287.                     {
  288.                         b.Jugg[j-k] = 9;
  289.                         k++;
  290.                     }
  291.                 }
  292.                 b.Jugg[j] = b.Jugg[j] + 10;
  293.                 c.Jugg.push_back(b.Jugg[j] - a.Jugg[i]);
  294.             }
  295.             j--;
  296.         }
  297.         while(j>=0)
  298.         {
  299.             c.Jugg.push_back(b.Jugg[j]);
  300.             j--;
  301.         }
  302.     }
  303.     return c;
  304. }
  305.  
  306. //Precondition: takes in a, b which are user defined
  307. //Postcondition: returns and anonymous Juggernaut that as the values of a*b
  308. Juggernaut operator *(Juggernaut a, Juggernaut b)
  309. {
  310.     char smaller = 'Z';
  311.     long int counter = 0;
  312.     int i = 0;
  313.     Juggernaut sum = Juggernaut("0");
  314.     while(smaller == 'Z' && i < a.size())
  315.     {
  316.         if(a.Jugg[i] > b.Jugg[i])
  317.             smaller = 'B';
  318.         else if(a.Jugg[i] < b.Jugg[i])
  319.             smaller = 'A';
  320.         else
  321.             i++;
  322.     }
  323.     if(a.size() >= b.size() || smaller == 'B')
  324.     {
  325.         for(int i = 0; i<b.size(); i++)
  326.             counter += (b.Jugg[i] * pow(10, (b.size()-1-i)));
  327.  
  328.         while(counter > 0)
  329.         {
  330.             sum = sum + a;
  331.             counter --;
  332.             sum.flip();
  333.         }
  334.     }
  335.     else if(a.size() < b.size() || smaller == 'A')
  336.     {
  337.         for(int i=0; i<a.size(); i++)
  338.             counter += (a.Jugg[i] * pow(10, (a.size()-1-i)));
  339.  
  340.  
  341.         while(counter > 0)
  342.         {    
  343.             sum = sum + b;
  344.             counter --;
  345.             sum.flip();
  346.         }
  347.     }
  348.     return sum;
  349. }
  350.  
  351. //Precondition: takes in user defined Juggernauts a and b
  352. //Postcondition: returns Juggernaut object with the values of a/b
  353. long int operator /(Juggernaut a, Juggernaut b)
  354. {
  355.     char smaller = 'Z';
  356.     long int counter = 0;
  357.     int i = 0;
  358.     Juggernaut temp;
  359.     while(smaller == 'Z' && i < a.size())
  360.     {
  361.         if(a.Jugg[i] > b.Jugg[i])
  362.             smaller = 'B';
  363.         else if(a.Jugg[i] < b.Jugg[i])
  364.             smaller = 'A';
  365.         else
  366.             i++;
  367.     }
  368.     if(a.size() >= b.size() || smaller == 'B')
  369.     {    
  370.         temp = a;
  371.         bool Help = temp.isNeg;
  372.         cout << temp.isNeg;
  373.         while(Help == false)
  374.         {
  375.             temp = temp - b;
  376.             Help = temp.Negative();
  377.             counter ++;
  378.             temp.flip();
  379.             temp.output();
  380.         }
  381.     }
  382.     else if(a.size() < b.size() || smaller == 'A')
  383.     {
  384.         temp = b;
  385.         while(b.Negative() == false)
  386.         {    
  387.             temp.flip();
  388.             temp = temp - a;
  389.             counter ++;
  390.         }
  391.     }
  392.     cout << "escape";
  393.     return counter;
  394. }
  395.  
  396. //Precondition: this is called after choice has been defined
  397. void menu::implement()
  398. {
  399.     if(choice == 1)//adds numbers, how this is done depends on the signs of the given numbers
  400.     {
  401.         getNums(); // ask user for values
  402.         if(a.Negative()==true && b.Negative() == true)
  403.         {
  404.             c=(a+b);//-a+-b
  405.             c.setNegative();// adding to negatives gives you a negative
  406.         }
  407.         else if(a.Negative() == true)
  408.         {
  409.             c=b-a; //-a+b
  410.         }
  411.         else if(b.Negative()==true)
  412.         {
  413.             c=a-b; // a+-b
  414.         }
  415.         else
  416.             c = a+b;
  417.         cout << "Your result is: ";
  418.         c.reverse(); //c is backwards so this flips them and displays them properly
  419.     }
  420.     else if(choice == 2)
  421.     {
  422.         getNums();    
  423.         if(a.Negative()==true && b.Negative()==true)
  424.             c=b-a; // -a--b
  425.         else if(a.Negative()==true)
  426.         {
  427.             c=a+b;//-a-b
  428.             c.setNegative();//subtracting a negative with a positive gives you a super negative
  429.         }
  430.         else if(b.Negative()==true)
  431.             c=a+b;//a--b
  432.         else
  433.             c = a-b;
  434.         cout << "Your result is: ";
  435.         c.reverse();//c is still backwards so flip it
  436.     }
  437.     else if(choice ==3)
  438.     {
  439.         getNums();
  440.         c=a*b;
  441.         if((a.Negative()==true && b.Negative()==false) || (a.Negative() == false && b.Negative() == true))
  442.             c.setNegative();
  443.         cout << "Your result is: ";
  444.         c.output();
  445.     }
  446.     else if(choice==4)
  447.     {
  448.         long int count;
  449.         getNums();
  450.         count=a/b;
  451.         cout << "Your result is: " << count;
  452.     }    
  453.     else if(choice==5){} // if choice is five then nothing happens and when it returns to menu
  454.     else                   //the program ends
  455.         cout << "\nInvailid Entry, Try again."; //error message
  456. }
  457.  
  458. //Precondition: Nothing has happened yet
  459. //Postcondition:It makes the program kick off and go
  460. int main()
  461. {
  462.     menu New; //creates a new menu
  463.     New.output();    // starts the interface
  464. }
Mar 23 '07 #1
2 1358
dmjpro
2,476 2GB
plz send the part of the code where the problem arises ........
Mar 23 '07 #2
DeMan
1,806 1GB
Sorry, can't see where you call isNeg

What does temp.isNeg output when you print it? anything?
Mar 23 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Kurt Watson | last post by:
I’m having a different kind of problem with Hotmail when I sign in it says, "Web Browser Software Limitations Your Current Software Will Limit Your Ability to Use Hotmail You are using a web...
7
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
7
by: tyler_durden | last post by:
thanks a lot for all your help..I'm really appreciated... with all the help I've been getting in forums I've been able to continue my program and it's almost done, but I'm having a big problem that...
23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
13
by: Joner | last post by:
Hello, I'm having trouble with a little programme of mine where I connect to an access database. It seems to connect fine, and disconnect fine, but then after it won't reconnect, I get the error...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
1
PEB
by: PEB | last post by:
POSTING GUIDELINES Please follow these guidelines when posting questions Post your question in a relevant forum Do NOT PM questions to individual experts - This is not fair on them and...
0
by: 2Barter.net | last post by:
newsmail@reuters.uk.ed10.net Fwd: Money for New Orleans, AL & GA Inbox Reply Reply to all Forward Print Add 2Barter.net to Contacts list Delete this message Report phishing Show original
6
by: jenipriya | last post by:
Hi all... its very urgent.. please........i m a beginner in oracle.... Anyone please help me wit dese codes i hv tried... and correct the errors... The table structures i hav Employee (EmpID,...
5
by: tabani | last post by:
I wrote the program and its not giving me correct answer can any one help me with that please and specify my mistake please it will be highly appreciable... The error arrives from option 'a' it asks...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.