入门C++体会(unexpected end of file while looking for precompiled header directive?)


unexpected end of file while looking for precompiled header directive
解决方法:
Project--Settings--C/C++--Precompiled Headers--Not using precompiled headers;

1).求用户提供的两个数之间的数的和(若v1=v2,该程序又怎能说正确呢?)
#include "stdafx.h"
#include <iostream.h>

int main()
{
 cout<<"Enter two numbers: "<<endl;
 int v1,v2;
 cin>>v1>>v2; //read input
 //use smaller number as lower bound for summation
 //and larger number as upper bound
 int lower,upper;
 if(v1<=v2)
 {
   lower=v1;
   upper=v2;
 } else {
  lower=v2;
  upper=v1;
 }
 
 int sum=0;
 //sum values from lower up to including upper
 for(int val=lower;val<=upper;++val)
  sum+=val;
 cout<<"Sum of"<<lower
  <<"to "<<upper
  <<"inclusive is"
  <<sum<<endl;
 return 0;
}

2).要求用户输入一组数,输出信息说明其中又多少个负数(aomnut=0,不初始化又怎能自加?)
#include "stdafx.h"
#include <iostream.h>

int main()
{
 cout<<"Please Enter Some numbers: "<<endl;
 int amount=0,value;
 while(cin>>value)
  if(value<=0)
   ++amount;
 cout<<"Amount of all negative values read is "
  <<amount<<endl;
 return 0;
}
3).对用户输入的一组数求和
#include "stdafx.h"
#include <iostream.h>

int main()
{
 int sum=0,value;
 //read till end-of-file,calculating a running total of all values read
 while(cin>>value)
  sum+=value;  //equivalent to sum=sum+value
 cout<<"Sum is: "<<sum<<endl;
 return 0;
}

4).将用户输入的两个数并将这两个数范围内的每个数写到标准输出
#include "stdafx.h"
#include <iostream.h>

int main()
{
 cout<<"Please Enter Two Numbers: "<<endl;
 int v1,v2;
 cin>>v1>>v2;  //read input

 //use smaller number as lower bound for summation
 //and larger number as upper bound
 int lower,upper;
 if(v1<=v2)
 {
  lower=v1;
  upper=v2;
 } else {
  lower=v2;
  upper=v1;
 }

 //value from lower up to upper
 for(int val=lower,count=1;val<=upper;++val,++count)
 {
  cout<<val<<" ";
  if(count%10==0)  //count that records the numbers of output,if count times 10,then output "endl";
   cout<<endl;
 }
 return 0;
}

 

5).当给16位的unsigned short对象赋值100000时,赋的值是什么?
 答:由于100000超过了16位的unsigned short类型的表示范围(2的16次方-1),编译器对其二进制表示截取低16位,相当于65536求余数(求模,%),得34464


6).要求用户输入两个数-底数(base)和指数(exponent),输出底数的指数次方的结果
#include "stdafx.h"
#include <iostream.h>

int main()
{
 //local varible
 int base,exponent;
 long result=1;

 //read input base and exponent
 cout<<"Enter base and exponent: "<<endl;
 cin>>base>>exponent;
 if(exponent<0)
 {
  cout<<"Exponent can't be smaller than 0"<<endl;
  return -1;
 }
 if(exponent>0)
 {
  //calculating base raised to the power of exponent
  for(int cnt=1;cnt<=exponent;++cnt)
   result*=base;
 }

 cout<<base
  <<"raised to the power of"
  <<exponent<<":"
  <<result<<endl;

 return 0;
}

7).从标准输入每次读入一行文本
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
 string line;
 //read line at time until end-of-file
 while(getline(cin,line))
  cout<<line<<endl;
 return 0;
}
8).从标准库输入每次读入一个单词
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
 string word;
 //read word at time until end-of-file
 while(cin>>word)
  cout<<word<<endl;
 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值