佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1109|回复: 18

C++ while loop 的问题

[复制链接]
发表于 9-10-2008 04:15 PM | 显示全部楼层 |阅读模式
我目前刚学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
回复

使用道具 举报


ADVERTISEMENT

发表于 9-10-2008 04:26 PM | 显示全部楼层
学过忘记了...
回复

使用道具 举报

发表于 9-10-2008 04:41 PM | 显示全部楼层
  1. #include <stdio.h>

  2. main()
  3. {
  4.         int quantity[9] = {0};
  5.         int price[9] = {0};
  6.         int i = 0;

  7.         while(i<9)
  8.         {
  9.                 quantity[i] = 10 + (i*10);
  10.                 price[i] = 50 + (i*50);
  11.                 i++;
  12.         }

  13.         i = 0;

  14.         printf("Quantity\tPrice\n\n");
  15.         while(i<9)
  16.         {
  17.                 printf("%d\t\t%d\n", quantity[i], price[i]);
  18.                 i++;
  19.         }
  20. }
复制代码
回复

使用道具 举报

发表于 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 = 设定空格的大小
回复

使用道具 举报

Follow Us
 楼主| 发表于 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 | 显示全部楼层

回复 7# AlexZana 的帖子

也可word<<"    "<<word
回复

使用道具 举报


ADVERTISEMENT

发表于 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我没法帮忙
回复

使用道具 举报


ADVERTISEMENT

发表于 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
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 23-12-2025 01:33 AM , Processed in 0.125621 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表