0
나는 2 장의 끝 부분에 연습용 마녀 1 명이 서있다. 이 연습 문제는 논리적으로 루프를 만들어 여러 번 물어 보는 방법을 알아낼 수 없다는 것입니다. 코드 마녀가 입력을 두 번 물어 썼습니다! 이전에 책이 제공 한 헤더로 나는 그 일을 쉬웠다. 그러나 그런 식으로는 더 이상 일하지 않았다. 이렇게 너에게 운동 및 부호를주고 너가 나를 도울 수 있길 바란다. 그리고 내 EnglsihC++ Primer Exercise 2.41
운동
쓰기 프로그램 마녀 죄송는 같은 장소 당신 main
기능의 클래스가됩니다.
코드 쓰기 마녀는 동일한 도서 번호로 여러 거래를 읽고 각 거래를 해당 도서 번호로 계산합니다.
내 코드도
#include <iostream>
#include <string>
using namespace std;
//Data structure Code
struct Sales_Data
{
std::string bookNo;
unsigned unit_sold;
double revenue;
};
int main()
{
Sales_Data data1,data2; //Data wich will hold input
double price; //Price per book used to calculate total revenue
// Checking if there was data input of book number units sold and price
if (std::cin>>data1.bookNo>>data1.unit_sold>>price)
{
int cnt=1; //Start Counter
data1.revenue=data1.unit_sold*price;// data1 calculating total revenue from price and unit_sold
while (std::cin>>data2.bookNo>>data2.unit_sold>>price)
{
data2.revenue=data2.revenue*price;
//checking if book name is same
if (data1.bookNo == data2.bookNo)
{
++cnt; //Incrementing counter if they same
unsigned totalCnt=data1.unit_sold+data2.unit_sold;
double totalRevenue=data1.revenue+data2.revenue;
//Print out result
std::cout<<cnt<<data1.bookNo<<" "<<totalCnt<<" "<<totalRevenue<<" ";
getchar();
getchar();
getchar();
if (totalCnt != 0)
std::cout<<totalCnt/totalRevenue;
else
std::cout<<"(No Sales)"<<std::endl;
return 0;
}else{
std::cerr<<"Book numbers isn't same"<<std::endl;
return -1;
}
}
}
return 0;
}
그리고하지만 수익 나에게 쓰레기 번호를 제공하는 이유가 낭비해야합니다. 감사합니다.
예'struct'에 있음 – AlexGreat
아니요, 선언이 아니고 정의가 아닙니다 ... 여기에'std :: cout << data2.revenue << std :: endl'을 시도해보고 결과를 확인하십시오. 쓰레기 야. – gongzhitaao
죄송합니다! 아니요, 데이터 멤버가 초기화 할 수 없다고 알려줍니다. – AlexGreat