저는 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': 선언되지 않은 식별자
함수 호출이 잘못되었습니다. 좋은 C++ 책을 읽으십시오. – bjskishore123