C++ 用“修改结构变量”;如果;报表C和x2B+;

C++ 用“修改结构变量”;如果;报表C和x2B+;,c++,struct,C++,Struct,明天我有一个家庭作业要交,它要求我们提示用户输入一些汽车,它们的行驶里程,然后显示总成本。成本取决于英里数;如果小于100,则为25美分/英里;如果大于100,则为100+15美分/英里。我创建了一个包含Miles和Price的结构。以下是我到目前为止的情况: #include "stdafx.h" #include <iostream> #include <string> using namespace std; struct Cars { int Miles

明天我有一个家庭作业要交,它要求我们提示用户输入一些汽车,它们的行驶里程,然后显示总成本。成本取决于英里数;如果小于100,则为25美分/英里;如果大于100,则为100+15美分/英里。我创建了一个包含
Miles
Price
的结构。以下是我到目前为止的情况:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

struct Cars
{
    int Miles;
    double Price;
};

int main()
{    
    cout << "ENTER ALL OF THE CARS!";
    int NoCars;
    cin >> NoCars;
    Cars* info = new Cars[NoCars];
    int i;
    for (i=0; i<NoCars; i++)
    {
        cout << "How many miles were driven on this car? :";
        cin >> info[i].Miles;
        if(Miles > 100)
        {
            Price = 25 + 0.15 * Miles;
        } 
        else 
        {
            Price = 0.25*Miles;
        }
    }
    cout << "Here are the prices: \n";
    for(x=0, x < NoCars; x++)
    {
        cout << info[x].Price;
    }

    return 0;
}
#包括“stdafx.h”
#包括
#包括
使用名称空间std;
结构车
{
国际英里;
双倍价格;
};
int main()
{    
cout>NoCars;
Cars*info=新车[NoCars];
int i;
对于(i=0;i信息[i]。英里;
如果(英里>100)
{
价格=25+0.15*英里;
} 
其他的
{
价格=0.25*英里;
}
}

cout
Price
Miles
struct Cars
的字段,因此您需要像这样使用它们:

if(info[i].Miles > 100)
{
    info[i].Price = 25 + 0.15 * info[i].Miles;
} 
else 
{
    info[i].Price = 0.25 * info[i].Miles;
}
if(info[i].Miles > 100)
    {
        info[i].Price = 25 + 0.15 * info[i].Miles;
    } 
    else 
    {
        info[i].Price = 0.25*info[i].Miles;
    }
for(x=0; x < NoCars; x++)
另外,在上次的
for
语句中,您使用的是未声明的
x
,可能是键入了
i
?顺便说一句,您在那里也丢失了一个分号:

for(x=0, x < NoCars; x++)
//     ^ should be ;
for(x=0,x
我认为这个小程序中几乎没有错误:

1:if(Miles > 100)
    {
        Price = 25 + 0.15 * Miles;
    } 
    else 
    {
        Price = 0.25*Miles;
    }
也许你应该这样改变:

if(info[i].Miles > 100)
{
    info[i].Price = 25 + 0.15 * info[i].Miles;
} 
else 
{
    info[i].Price = 0.25 * info[i].Miles;
}
if(info[i].Miles > 100)
    {
        info[i].Price = 25 + 0.15 * info[i].Miles;
    } 
    else 
    {
        info[i].Price = 0.25*info[i].Miles;
    }
for(x=0; x < NoCars; x++)
2:
for(x=0,x

也许你应该这样改变:

if(info[i].Miles > 100)
{
    info[i].Price = 25 + 0.15 * info[i].Miles;
} 
else 
{
    info[i].Price = 0.25 * info[i].Miles;
}
if(info[i].Miles > 100)
    {
        info[i].Price = 25 + 0.15 * info[i].Miles;
    } 
    else 
    {
        info[i].Price = 0.25*info[i].Miles;
    }
for(x=0; x < NoCars; x++)
for(x=0;x
Price在结构Cars中。你不能“简单地”访问它。(是的,你需要使用指针。)
Price
Cars
结构的成员,你不能只说
Price
就指望编译器知道你指的是哪一个。同样的
Miles
(你可以正确地访问它来设置它)在这里得到访问:<代码>信息[i]。迈尔斯<代码>但下一行错误:<代码>(英里)100 /代码>,顺便说一下,工业标准编码约定是使用引号小写来命名变量和方法名称,同时保留结构名、类名和(某些)常量的引头大写。这是java编码约定,而不是C++。