2013-05-13 4 views
0

저는 C++을 처음 사용하고 있으며 문자열을 내 코드의 기본 클래스로 다시 전달하는 데 문제가 있습니다.반환 된 함수를 호출하는 데 문제가 있습니다.

내 목표는 내가 메인 클래스가 아닌 다른 두 기능을 가지고 적어도 하나의 코드부터 0

이외의 값을 반환해야합니다 있도록 아래의 코드를 분할하는 것입니다

#include "stdafx.h" 
#include <iostream> 
#include <iomanip> 
#include <string> 

using namespace std; 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    cout.precision(2); 
    cout.setf(ios::fixed,ios::floatfield); 

    float speedLimit; 
    float driversSpeed; 
    float ticketAmount; 
    float speedOver; 
    string repeat; 

    /*Use a symbolic constant for the base ticket fine rate ($50).*/ 
    const float base = 50; 

    start: 
    /*Prompt the user for the speed limit and the speed of the driver.*/ 
    cout << "Enter the speed limit: "; 

    cin >> speedLimit; 

    cout << "Enter the driver's speed: "; 

    cin >> driversSpeed; 


    cout << "You were driving " << driversSpeed << " in a " << speedLimit << " mph zone.\n"; 

     speedOver = driversSpeed - speedLimit; 


     if (speedOver <= 10 && speedOver >= 1) 
     { 
     ticketAmount = base; 
     } 

     else if (speedOver <= 14 && speedOver >= 11) 
     { 
     ticketAmount = (base *.05) + base; 
     } 

     else if (speedOver <= 19 && speedOver >= 15) 
     { 
     ticketAmount = (base *.1) + base; 
     } 

     else if (speedOver <= 24 && speedOver >= 20) 
     { 
      ticketAmount = (base *.15) + base; 
     } 

     else if (speedOver <= 29 && speedOver >= 25) 
     { 
      ticketAmount = (base *.2) + base; 
     } 

     else if (speedOver >= 30) 
     { 
     ticketAmount = (base *.25) + base; 
     } 

     else 
     { 
      ticketAmount = 0; 
     } 

     cout << "Your fine is $" << ticketAmount; 


     cout << "\nEnter Y to continue. Anything else to stop: "; 

     cin >> repeat; 

     if (repeat == "Y" || "y") 
      goto start; 
     else 
      exit(0); 

     return 0; 
} 

및 여기에 지금까지 무엇을했는지 :

:

#include "stdafx.h" 
#include <iostream> 
#include <iomanip> 
#include <string> 

using namespace std; 

const float base = 50; 
float speedLimit; 
    float driversSpeed; 
    float ticketAmount; 
    float speedOver; 


int _tmain(int argc, _TCHAR* argv[]) 
{ 
    cout.precision(2); 
    cout.setf(ios::fixed,ios::floatfield); 


    string repeat; 

    /*Use a symbolic constant for the base ticket fine rate ($50).*/ 


    start: 
    /*Prompt the user for the speed limit and the speed of the driver.*/ 
    cout << "Enter the speed limit: "; 

    cin >> speedLimit; 

    cout << "Enter the driver's speed: "; 

    cin >> driversSpeed; 

    /*Display to the user the values which were input (speed limit and driver's speed) and the calculated ticket fine amount. Print 2 numbers after the decimal point for the fine amount. Make sure your output format matches the sample format.*/ 

    cout << "You were driving " << driversSpeed << " in a " << speedLimit << " mph zone.\n"; 

     speedOver = driversSpeed - speedLimit; 


     cout << string(finalOutput); 

     /*After the fine is printed for the first speeding violation, prompt the user to see if he/she wants to enter another speeding violation. If so, prompt again for the speed limit and driver's speed. Repeat the calculation and print the fine. Repeat this process until the user indicates he/she wants to stop. The user can enter either an uppercase or lowercase letter Y to continue with the program.*/ 

     cout << "\nEnter Y to continue. Anything else to stop: "; 

     cin >> string(repeat); 

     if (repeat == "Y" || "y") 
      goto start; 
     else 
      exit(0); 


} 

float ticketAmountFunc(float ticketAmount) 
{ 
      /*Calculate the ticket cost as $50 (the base fine rate) plus: 
0% additional if the driver's speed was 10 or less miles per hour above the speed limit. 
5% additional if driver's speed was more than 10 miles per hour above the speed limit. 
10% additional if driver's speed was more than 15 miles per hour above the speed limit 
15% additional if driver's speed was more than 20 miles per hour above the speed limit. 
20% additional if driver's speed was more than 25 miles per hour above the speed limit. 
25% additional if driver's speed was 30 or more miles per hour above the speed limit. 
Do not charge a fine if the driver wasn't speeding.*/ 

     if (speedOver <= 10 && speedOver >= 1) 
     { 
     ticketAmount = base; 
     } 

     else if (speedOver <= 14 && speedOver >= 11) 
     { 
     ticketAmount = (base *.05) + base; 
     } 

     else if (speedOver <= 19 && speedOver >= 15) 
     { 
     ticketAmount = (base *.1) + base; 
     } 

     else if (speedOver <= 24 && speedOver >= 20) 
     { 
      ticketAmount = (base *.15) + base; 
     } 

     else if (speedOver <= 29 && speedOver >= 25) 
     { 
      ticketAmount = (base *.2) + base; 
     } 

     else if (speedOver >= 30) 
     { 
     ticketAmount = (base *.25) + base; 
     } 

     else 
     { 
      ticketAmount = 0; 
     } 

     return ticketAmount; 
} 

string finalOutput(string tix) 
{ 
    string words = "Your fine is $"; 
    //tix = words + ticketAmountFunc; 

    tix += string(words) + string(ticketAmountFunc); 

    return tix; 
} 

VS 2 오류를 반환 표준 : : 문자열 ''에서 '

loat)

누군가가 내 오류의 방향으로 날 세스 지점시겠습니까?

감사합니다.

편집 : Ben 감사합니다. 필자는 주된 방법을 옮기고 움직이는 변수를 사용하여 문자열로 선언했지만 아직 선언되지 않은 식별자 문제는 있지만 두 번이나 나타냅니다. 여기

내 업데이트 된 코드입니다 :

#include "stdafx.h" 
#include <iostream> 
#include <iomanip> 
#include <string> 

using namespace std; 

const float base = 50; 
float speedLimit; 
    float driversSpeed; 
    float ticketAmount; 
    float speedOver; 




string ticketAmountFunc(string r) 
{ 


    string ticketAmount; 



     if (speedOver <= 10 && speedOver >= 1) 
     { 
     ticketAmount = base; 
     } 

     else if (speedOver <= 14 && speedOver >= 11) 
     { 
     ticketAmount = (base *.05) + base; 
     } 

     else if (speedOver <= 19 && speedOver >= 15) 
     { 
     ticketAmount = (base *.1) + base; 
     } 

     else if (speedOver <= 24 && speedOver >= 20) 
     { 
      ticketAmount = (base *.15) + base; 
     } 

     else if (speedOver <= 29 && speedOver >= 25) 
     { 
      ticketAmount = (base *.2) + base; 
     } 

     else if (speedOver >= 30) 
     { 
     ticketAmount = (base *.25) + base; 
     } 

     else 
     { 
      ticketAmount = "0"; 
     } 
    std::string s = ticketAmount; 
    r = s; 
     return r; 
} 

string finalOutput(string tix) 
{ 
    string words = "Your fine is $"; 
    //tix = words + ticketAmountFunc; 


    tix = string() + words + ticketAmountFunc(r); 



    return tix; 
} 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    cout.precision(2); 
    cout.setf(ios::fixed,ios::floatfield); 


    string repeat; 

    /*Use a symbolic constant for the base ticket fine rate ($50).*/ 


    start: 
    /*Prompt the user for the speed limit and the speed of the driver.*/ 
    cout << "Enter the speed limit: "; 

    cin >> speedLimit; 

    cout << "Enter the driver's speed: "; 

    cin >> driversSpeed; 



    cout << "You were driving " << driversSpeed << " in a " << speedLimit << " mph zone.\n"; 

     speedOver = driversSpeed - speedLimit; 


     cout << string(finalOutput(tix)); 



     cout << "\nEnter Y to continue. Anything else to stop: "; 

     cin >> string(repeat); 

     if (repeat == "Y" || "y") 
      goto start; 
     else 
      exit(0); 


} 

내 오류는 다음과 같습니다

오류 7 오류 C2065 : 'R': 선언되지 않은 식별자
오류 8 오류 C2065 : 'TIX': 선언되지 않은 식별자

+0

함수 호출이 잘못되었습니다. 좋은 C++ 책을 읽으십시오. – bjskishore123

답변

0

"선언 시점"전에 함수를 사용하려고합니다. 당신이 그것을 사용하기 전에

  1. 프로토 타입 일명 앞으로 선언을 추가 : 두 가지 간단한 솔루션 (하나를 선택)이있다.
  2. 호출자 (main())를 사용하는 기능 아래로 이동하십시오.

또한 함수를 호출 할 때 괄호 안에 인수를 제공해야합니다. 따라서 이 아니라 ticketAmountFunc(ticketAmount)이라고 말해야합니다.

이러한 문제 외에도 함수 매개 변수를 사용하는 데이터 대신 함수가 생성하는 값으로 정의하는 것처럼 보입니다. 유용하지 않습니다. 함수를 잘 사용하면 전역 변수가 필요하지 않습니다.

0

그냥 추가 할 수 있다면 경력의이 단계에서 goto 문을 사용하지 말고 적절한 루프와 함수를 사용하십시오.

이것은 담요 규칙이 아니지만 매우 구체적인 & 유효한 이유가없는 한 goto를 피할 수 있습니다.

이 같은 루프를 사용할 수 있습니다

bool Quit = false; 

while(!Quit) { 


    // user wants to quit 
    Quit = true; // execution continues after end of while loop 
} 

또한 보수 ToUpper 기능을 활용, "++의 ToUpper C"구글 당신이 문자의 값에 2 테스트를하지 않아도됩니다.

가능한 경우 using namespace std은 함수명 & 변수 이름과 충돌 할 수있는 저속한 네임 스페이스를 오염 시키므로 피해야합니다. 예를 들어 STL에는 거리와 같은 모든 종류의 일반적인 단어가 있습니다. 왼쪽에는 & 등 특수 의미가 있습니다. 그러니 각 표준 일 전에 std::을 넣어, 또는 자주 사용하는 물건에 대해이 작업을 수행 :

using std::cout; 
using std::cin; 
using std::endl; 
using std::string; 

일부 사람들은 exclusivley std::를 사용, 가끔이 두 개념의 혼합물을 할 수있다.

희망은 모두 잘됩니다.

관련 문제