|
查看: 1109|回复: 18
|
C++ while loop 的问题
[复制链接]
|
|
|
我目前刚学C++,我要建一个table;
大至上是这样;要用while loop....有那一位高手可以教我?
table的线条可以ignore.
| Quantity | Price | | 10 | 50 | | 20 | 100 | | 30 | 150 | | 40 | 200 | | 50 | 250 | | 60 | 300 | | 70 | 350 | | 80 | 400 | | 90 | 450
|
|
|
|
|
|
|
|
|
|
|
|
发表于 9-10-2008 04:26 PM
|
显示全部楼层
学过忘记了... |
|
|
|
|
|
|
|
|
|
|
发表于 9-10-2008 04:41 PM
|
显示全部楼层
- #include <stdio.h>
- main()
- {
- int quantity[9] = {0};
- int price[9] = {0};
- int i = 0;
- while(i<9)
- {
- quantity[i] = 10 + (i*10);
- price[i] = 50 + (i*50);
- i++;
- }
- i = 0;
- printf("Quantity\tPrice\n\n");
- while(i<9)
- {
- printf("%d\t\t%d\n", quantity[i], price[i]);
- i++;
- }
- }
复制代码 |
|
|
|
|
|
|
|
|
|
|
发表于 9-10-2008 04:45 PM
|
显示全部楼层
不是高手,不过应该是酱:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
int i=10;
cout<<setiosflags(ios::left);
cout<<setw(10)<<"Quantity"<<setw(10)<<"Price"<<endl;
while(i<100)
{
cout<<setw(10)<<i<<setw(10)<<i*5<<endl;
i+=10;
}
getch();
} |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 9-10-2008 04:50 PM
|
显示全部楼层
可以解释什么是setiosflags(ios::left);
还有setw
谢谢哦 |
|
|
|
|
|
|
|
|
|
|
发表于 9-10-2008 04:56 PM
|
显示全部楼层
原帖由 AlexZana 于 9-10-2008 04:50 PM 发表 
可以解释什么是setiosflags(ios::left);
还有setw
谢谢哦
如果没错的话:
setiosflags = Set Input Output Stream Flag
ios::left = 字从左边开始显示。
setw = Set Width = 设定空格的大小 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 9-10-2008 05:00 PM
|
显示全部楼层
原帖由 alexander3133 于 9-10-2008 04:56 PM 发表 
如果没错的话:
setiosflags = Set Input Output Stream Flag
ios::left = 字从左边开始显示。
setw = Set Width = 设定空格的大小
哦,原来。。。谢谢各位。。。我一直弄不到空位(space between words)..原来要用setw |
|
|
|
|
|
|
|
|
|
|
发表于 9-10-2008 05:16 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 9-10-2008 05:23 PM
|
显示全部楼层
WAH, alexander313& 避风港的鱼好厉害,我做了个烂答案。。。
#include<stdio.h>
int main()
{
int quantity,price=50;
printf("Quantity \t Price\n");
quantity=10;
while(quantity<=90)
{
printf("%d \t\t %d\n",quantity,price);
price=price+50;
quantity=quantity+10;
}
return 0;
}
[ 本帖最后由 kingdom_manga 于 9-10-2008 05:24 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 9-10-2008 06:57 PM
|
显示全部楼层
不好意思楼主, 借你平台下。请问为啥我只用#include<stdio.h>不能RUN的?一定得有#include<iostream.h>, #include<conio.h>
getch();? |
|
|
|
|
|
|
|
|
|
|
发表于 9-10-2008 07:32 PM
|
显示全部楼层
|
cout , ios 等的 stream manipulator, stream function 只存在与 iostream library 里面。 setw 等是在 iomanip 里面 |
|
|
|
|
|
|
|
|
|
|
发表于 9-10-2008 09:44 PM
|
显示全部楼层
原帖由 避风港的鱼 于 9-10-2008 06:57 PM 发表 
不好意思楼主, 借你平台下。请问为啥我只用#include不能RUN的?一定得有#include, #include
getch();?
不好意思,#include <stdio.h> 只是用在C而非C++,所以不能用,我看太快了,没看到C++,所以就做C.
在C++里,#include <iostream.h>是一定要有的。conio.h如果没错的话好像是给getch()。
getch()用是为了你必须按随便一个键后,program才会exit, 如果没有getch()的话,当你run它时,他跑完后自动会exit,然后你就问:为什么program一下子就不见了? |
|
|
|
|
|
|
|
|
|
|
发表于 10-10-2008 04:19 PM
|
显示全部楼层
其实我之前看一本04年的书,里面写:
#include<iostream>
using namespace std;
int main()
{
body
return 0;
}
可是照抄都不能RUN? |
|
|
|
|
|
|
|
|
|
|
发表于 10-10-2008 04:36 PM
|
显示全部楼层
原帖由 避风港的鱼 于 2008-10-10 04:19 PM 发表 
其实我之前看一本04年的书,里面写:
#include
using namespace std;
int main()
{
body
return 0;
}
可是照抄都不能RUN?
body 的意识就是你得coding
没有coding怎样run? |
|
|
|
|
|
|
|
|
|
|
发表于 10-10-2008 06:36 PM
|
显示全部楼层
回复 14# 悲哀河川 的帖子
|
什么是CODING?BODY是我自己省略没写的东西。 |
|
|
|
|
|
|
|
|
|
|
发表于 10-10-2008 08:34 PM
|
显示全部楼层
应该是没有getch()吧,
没有全部的coding我没法帮忙 |
|
|
|
|
|
|
|
|
|
|
发表于 10-10-2008 08:39 PM
|
显示全部楼层
回复 15# 避风港的鱼 的帖子
compiler error message 贴上来,
code 贴上来 |
|
|
|
|
|
|
|
|
|
|
发表于 11-10-2008 10:53 PM
|
显示全部楼层
回复 12# alexander3133 的帖子
#include <stdio.h>
再C++也可以用啊啊。。
不过一定要有#include<iostream.h> |
|
|
|
|
|
|
|
|
|
|
发表于 15-10-2008 11:07 AM
|
显示全部楼层
回复 17# onlylonly 的帖子
#include<iostream>
using namespace std;
int main()
{
cout<<"Haha"<<endl;
return 0;
}
[C++Warning]Unit1.cpp(2): W8058 Cannot create pre-compiled header: write failed |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|