首页 > 解决方案 > C++ 错误问题:Expected Unqualified-id

问题描述


#include <iostream>
#include <algorithm>
#include <string>

public static void main (String[]args)
{
    int dozen=12;
    double PricePerDozen = 3.25;
    double PricePerEgg = 0.45;
    int eggs, dozens, leftover;
    double finalTotal;
    Scanner input = new Scanner(System.in);
    System.out.print("Enter number eggs needed>>");
    eggs= input.nextInt();
    dozens= eggs/dozen;
    leftOver= eggs%dozen;
    finalTotal= dozens*PricePerDozen+leftOver
*PricePerEgg;
    System.out.println("You order " + eggs + " eggs. That's " dozen and " dozen at $ " + PricePerDozen + " per dozen and " +leftOver +" loose eggs at " + (PricePerEgg*100) + " cents each for a total of $ " +finalTotal):
}

我收到一个错误:public static void main (string{}args) 行的预期 unqualified-id。不知道出了什么问题,因为我是 C++ 和一般编码的新手。

顺便说一句,代码是针对这个问题的:

一家农场向当地客户出售鸡蛋。一打鸡蛋收费 3.25 美元,不属于一打的单个鸡蛋收费 45 美分。

编写一个程序,提示用户输入订单中的鸡蛋数量,然后显示欠款金额。(将价格声明为常数)


感谢您的回复,我是新手,所以我不知道自己在做什么,但我更正了正确的 c++ 格式。这就是我所做的:

#include<iostream>
using namespace std;
 
int main()
{
  int noOfEggs;
  const double pricePerDozen=3.25;
  const double pricePerEgg=0.45;
  double totalPrice;
  int noOfDozes,noOfLeftEggs;
  
  cout<<"How many eggs would you like?:";
cin>>noOfEggs;
  noOfDozes=noOfEggs/12;
  noOfLeftEggs=noOfEggs%12;
  
  totalPrice=(noOfDozes*pricePerDozen)+(noOfLeftEggs*pricePerEgg);
  cout<<noOfDozes<<" dozens and "<<noOfLeftEggs<<" eggs cost: $"<<totalPrice<<endl;
  return 0;

标签: c++

解决方案


这可能来自函数String参数中的单词main()。编译器没有已知的类、函数、变量等命名String,因此会出错。你可能会从这个程序中得到同样的错误:

int main() {
    String s;
    return 0;
}

在后一种情况下,如果添加一个名为 的类String,则可以编译它:

class String {};

int main() {
    String s;
    return 0;
}

正如cigien在评论中提到的那样,您的代码看起来像 Java 代码并且不是有效的 C++,因此在编译之前您还有很多事情需要修复。解决所有这些问题超出了本网站的范围。我刚刚提到了您询问的一个错误的可能原因。


推荐阅读