Results 1 to 7 of 7

Thread: [RESOLVED] Two issues

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    112

    Resolved [RESOLVED] Two issues

    OK, I am having two issues right now. In the first program, I am trying to calculate the distance between two points. I think I have the right formula, but I am not sure. I am supposed to use the pow and sqrt functions, and I have tried. The output is something crazy.

    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    
    using namespace std;
    
    int main()
    {
        int x1;
        int x2;
        int y1;
        int y2;
        int d1;
        int d2;
        int d3;
        double distance;
        
        cout << "What is the x value for point 1?" << endl;
        cin >> x1;
        
        cout << "What is the y value for point 1?" << endl;
        cin >> y1;
        
        cout << "What is the x value for point 2?" << endl;
        cin >> x2;
        
        cout << "What is the y value for point 2?" << endl;
        cin >> y2;
        
        distance = sqrt(sqrt(x2-x1) + sqrt(y2-y1));
        
        cout << distance << endl;
        
        
    system("pause");
    return 0;
    
    }
    As for my 2nd problem, I am having issues with if else statements. I am supposed to be making an error handler for an invalid rating( not equal to 1, 2 or 3)...I have tried to do so but I guess I am just not getting it. I am new to C++ as you can see. Please help in anyway possible:

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main()
    { 
      double currentSalary ;            //input by user
      double raise ;                     //calculated by program based on the rating
      double newSalary ;                 // calculated by the program;currentSalary + raise 
      int rating ;                      // input by the user 
      string employee ;                 // the name of the employee
      
      
      // initialize raise and newSalary
      
      raise =0;
      newSalary = 0;
      
      cout << " Enter the employee's name and press enter   " ;
      getline ( cin, employee);
      
      cout << "Enter the current salary \t" ;
      cin >> currentSalary;
    
      cout << "Input a performance rating \n "
              "\t1 excellent \n\t2 good \n\t3 poor\n";
      cin >> rating ;
         
      if (rating = 1)
      
                newSalary = .06 * currentSalary;
                cout << "New Salary: " << currentSalary << endl;
                cout << "Raise Amount: 6%"  << endl;
                cout << "New Salary: " << newSalary << endl;
      
      if (rating = 2)
      
      
                newSalary = .04 * currentSalary;
                cout << "New Salary: " << currentSalary << endl;
                cout << "Raise Amount: 4%"  << endl;
                cout << "New Salary: " << newSalary << endl;
      
      if (rating = 3)
      
      
                newSalary = .015 * currentSalary;
                cout << "New Salary: " << currentSalary << endl;
                cout << "Raise Amount: 1.5%"  << endl;
                cout << "New Salary: " << newSalary << endl;
       
       if (rating != 1,2,3)
         cout << "Invalid rating" << endl;
    
    
                
      
      
      // Include if (if else) to calculate the raise and the new salary. 
      
      
      
      
    
      //cout << fixed << showpoint<<setprecision(2);
      //cout << "\n\nEmployee name: " << employee ;
      //cout << "\ncurrent salary\t$"<< currentSalary;
      //cout << "\nraise \t$"<< raise <<" for performance rating " << rating ; 
      //cout << "\nnew salary\t$"<< newSalary << "\n\n\n";
    
      system("pause");
    }

  2. #2
    Addicted Member
    Join Date
    Jul 2001
    Posts
    158

    Re: Two issues

    you can't write if statements this way unless there is only one line following the statement that you want to be executed
    Code:
    //WRONG
    if (rating = 1)
      
                newSalary = .06 * currentSalary;
                cout << "New Salary: " << currentSalary << endl;
                cout << "Raise Amount: 6%"  << endl;
                cout << "New Salary: " << newSalary << endl;
    
    //RIGHT
    if (rating == 1){
      
                newSalary = .06 * currentSalary;
                cout << "New Salary: " << currentSalary << endl;
                cout << "Raise Amount: 6%"  << endl;
                cout << "New Salary: " << newSalary << endl;
    }
    
    //ALSO RIGHT
    if (rating == 1)  
                cout << "New Salary: " << newSalary << endl;
    
    //AND USE THIS FORM FOR IF/ELSE
    if (rating == 1){
       //..
    }else if (rating == 2){
       //..
    }else if(...){
       //..
    }
    also notice you're conditional was incorrect 'rating=1' sets rating to 1, you need '=='. there may be other errors that i'm missing, but that's a start

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    112

    Re: Two issues

    Ah, thanks a lot..I didn't even notice those. I got the rating part working, just working on the distance one now

  4. #4
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Two issues

    distance = sqrt(pow(x2-x1, 2) + pow(y2-y1, 2))

    Edited - sorry I put in too many close parentheses.
    Last edited by wossname; Sep 28th, 2008 at 11:48 AM.
    I don't live here any more.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    112

    Re: Two issues

    I seem to be getting an error. "No matching function for call to pow(int). I think it may have something to do with the ", 2" but I am not sure.

  6. #6
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Two issues

    My fault, see my above post again, I've edited it a bit.
    I don't live here any more.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    112

    Re: Two issues

    I think I got it. Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width