iT邦幫忙

1

痾 有關c++for迴圈的問題

//食為a,衣為b,c為住,d為行,e為育,f為樂 
#include<iostream>
#include<cstdlib>
#include<string>
#include<vector> 
using namespace std;
int food(int);
int clothes(int);
int live(int);
int transportation(int);
int teach(int);
int happy(int);
int income(int);
int main()
{
    int foodprice;
	food(foodprice);
	cout<<endl;
	
	int g; int clothesprice;
	cout<<"穿著方面"<<endl;  
	cout<<"衣著類 1.有購買 2.沒有購買"<<endl;
	cin>>g;
	if(g==1){clothes(clothesprice);}
	cout<<endl;
	
	int h;int liveprice;
	cout<<"住的方面"<<endl;
	cout<<"家具水電等費用 1.有購買 2.沒有購買"<<endl;
	cin>>h;
	if(h==1){
	live(liveprice);}
	cout<<endl;
	
	int m;int transportationprice;
	cout<<"行的方面"<<endl;
	cout<<"交通等費用 1.有購買 2.沒有購買"<<endl;
	cin>>m;
	if(m==1){
	transportation(transportationprice);}
	cout<<endl;
	
	int n;int teachprice;
	cout<<"育的方面"<<endl;
	cout<<"補習等等 1.有購買 2.沒有購買"<<endl; 
	cin>>n; 
	if(n==1){teach(teachprice);} 
	cout<<endl;
	
	int o;int happyprice;
	cout<<"樂的方面"<<endl;
	cout<<"娛樂等等 1.有購買 2.沒有購買"<<endl; 
	cin>>o;
	if(o==1){
	happy(happyprice);}
	cout<<endl;
	
	int work,s;
	income(work);
	cout<<"請輸入一天生活費"<<endl;
	cin>>s; 
	
	
	int W;
	W=work-(foodprice+clothesprice+liveprice+transportationprice+teachprice+happyprice);
	cout<<"你的收入-支出="<<s+work-(foodprice+clothesprice+liveprice+transportationprice+teachprice+happyprice)<<endl;
	if(W>0){cout<<"你今天有存到錢哦";}
	else if(W<0){cout<<"\a\a\a\a\a\a\a你負債了!明天繼續加油!!";}
	
	system("PAUSE");
	return 0;
}

int food(int total1)
{	
    vector<string> food(6);
	int a[6]={0};
	cout<<"今天吃了甚麼?"<<endl; 
	cout<<"1.brakefast name  價錢"<<endl;
	cin>>food[1]>>a[1];
	cout<<"2.lunch name  價錢"<<endl;
	cin>>food[2]>>a[2];
	cout<<"3.dinner name  價錢"<<endl;
	cin>>food[3]>>a[3];
	cout<<"請輸入 ,4.零食name(按enter),5.其他花費"<<endl;
	for(int i=4;i<6;i++){
		cin>>food[i]>>a[i];
	}
    for(int i=1;i<6;i++)
	{
    total1+=a[i];
	}	
	cout<<"總共花了"<<total1<<endl;
	
	return total1;
}

int clothes(int total2)
{
	int N;
	cout<<"請輸入購買種類(n?):";cin>>N;
	vector <string> clothes(N);
	int b[N]={0};
	cout<<"請輸入物品及金額"<<endl;
	int l=0; 
	while(l<N)
	{
		cout<<"種類:";cin>>clothes[l];cout<<": 總共金額:"; cin>>b[l];
		total2+=b[l];
		l++; 
	}
    
	cout<<"你在衣著類花了"<<total2-61<<"元"<<endl;  
	return total2-61; 
}
int live(int total3)
{
	int N;
	cout<<"請輸入購買種類(n?):";cin>>N;
	vector <string> live(N);
	int c[N]={0};
	cout<<"請輸入物品及金額"<<endl; 
	for(int i=0;i<N;i++)
	{
		cout<<"種類:";cin>>live[i];cout<<": 總共金額:"; cin>>c[i]; 
	    total3+=c[i];
	}
    cout<<endl;
	cout<<"你在住的方面花了"<<total3<<"元"<<endl; 
	return total3; 
}
int transportation(int total4)
{
    int N;
	cout<<"請輸入搭乘或其他種類(n?):";cin>>N;
	vector <string> transportation(N);
	int d[N]={0};
	cout<<"請輸入金額"<<endl; 
	for(int i=0;i<N;i++)
	{
		cout<<"種類:";cin>>transportation[i];cout<<": 總共金額:"; cin>>d[i]; 
	    total4+=d[i];
	}

	cout<<"你在行的方面花了"<<total4<<"元"<<endl; 
	return total4; 
}
int teach(int total5)
{
	int N;
	cout<<"請輸入購買種類(n?):";cin>>N;
	vector <string> teach(N);
	int e[N]={0};
	cout<<"請輸入物品及金額"<<endl; 
	for(int i=0;i<N;i++)
	{
		cout<<"種類:";cin>>teach[i];cout<<": 總共金額:"; cin>>e[i]; 
	    total5+=e[i];
	}
	cout<<endl;
	cout<<"你在育的方面花了"<<total5<<"元"<<endl; 
	return total5; 
  
}
int happy(int total6)
{  
	int N;
	cout<<"請輸入購買種類(n?):";cin>>N;
	vector <string> happy(N);
	int f[N]={0};
	cout<<"請輸入物品及金額"<<endl;
	int j=0; 
	while(j<N)
	{
		cout<<"種類:";cin>>happy[j];cout<<": 總共金額:"; cin>>f[j]; 
	    total6+=f[j];
	    j++;
	}
    cout<<"你的玩樂花費為"<<total6-4259529<<"元"<<endl;
	return total6-4259529;
}
int income(int total7)
{
	int p,q,r;
	cout<<"是否有打工?1.是2.否"<<endl;
	cin>>p;
	if(p==1){
	cout<<"請輸入幾個小時"<<endl;
	cin>>q;
	cout<<"請輸入時薪"<<endl;
	cin>>r;
	total7=q*r;
	cout<<"你今天的收入為"<<total7<<endl; 
	} cout<<endl; 
	return total7;
}

不知道為甚麼 有些迴圈的加總是對的 有些會錯的很離譜 我還是個新手
查了許多資料還是不太清楚 好像是相似的程式馬不能太多 但是這樣要怎麼做才好呢QQ
乞求各位協助了/images/emoticon/emoticon02.gif/images/emoticon/emoticon02.gif/images/emoticon/emoticon02.gif/images/emoticon/emoticon41.gif

剛剛有改了一些地方 還是怪怪的 .............QQ

丹尼 iT邦研究生 4 級 ‧ 2017-01-13 17:27:03 檢舉
怎麼會呢。 邏輯思考 背後都數學運算
多翻閱電腦書籍。
丹尼 iT邦研究生 4 級 ‧ 2017-01-13 17:47:30 檢舉
進銷存的書籍去翻一翻 看你寫的好長又好奇怪
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

7
perfectcamel
iT邦見習生 ‧ 2017-01-14 02:22:55
最佳解答

主要是下面兩個問題:

  1. Dev-C++裡,如果變數不給定預設值的話,是不能確定他的起始值的。

以你的例子來說,所有price都應該要初始化為0,像是:

int foodprice = 0;
  1. C++變數是預設傳值的,意思就是你在呼叫副函式的時候傳入的變數並不會被改變,只會把當時裡面的值交付過去。

所以你的例子中,foodprice是會維持不變的,應該改為以下的寫法,把food的回傳內容存入foodprice,他才會變動:

int foodprice = 0;
foodprice = food(foodprice);

實際上,由於傳入的數字是不是該變數根本無關,這個副函式實際上沒有傳入數字的必要,所以你的int food(int)應該改寫為int food():

int food()
{	
    int total1 = 0;
    vector<string> food(6);
	int a[6]={0};
	cout<<"今天吃了甚麼?"<<endl; 
	cout<<"1.brakefast name  價錢"<<endl;
	cin>>food[1]>>a[1];
	cout<<"2.lunch name  價錢"<<endl;
	cin>>food[2]>>a[2];
	cout<<"3.dinner name  價錢"<<endl;
	cin>>food[3]>>a[3];
	cout<<"請輸入 ,4.零食name(按enter),5.其他花費"<<endl;
	for(int i=4;i<6;i++){
		cin>>food[i]>>a[i];
	}
    for(int i=1;i<6;i++)
	{
    total1+=a[i];
	}	
	cout<<"總共花了"<<total1<<endl;
	
	return total1;
}

這樣的話,主函式記得改成:

int foodprice = food();

主要的邏輯問題應該就是這兩個,流程的部分就不做評價了,另外,裡面一些很奇怪的變數加減記得拿掉,我猜你是因為沒有初始化造成出現的數字很奇怪,所以手動加入了那些-4259529或-61的吧?

大致上是這樣。

專業的好心人
給您按個/images/emoticon/emoticon12.gif/images/emoticon/emoticon12.gif

比那些只會亂槍打鳥,明明不會卻又佔著回答位置的人好上1萬倍。

小k iT邦新手 4 級 ‧ 2017-01-16 08:32:16 檢舉

很優質的前輩!

WilliamHuang
iT邦研究生 1 級 ‧ 2017-01-13 17:21:05
【**此則訊息已被站方移除**】
0
小茶
iT邦新手 4 級 ‧ 2017-01-14 01:44:22

好像是沒有聽過相似程式碼不能太多啦...
不過能簡就簡會提升不少效率,
for loop 的邏輯很容易修正唷,
最簡單的方式就是運用 breakpoint debug 就行啦~
看哪邊錯再用單步去除錯

我要發表回答

立即登入回答