C++ Tutorial/Data Types/int read

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Read int and char array from keyboard

#include <iostream>  
#include <stdio.h>  
using namespace std;
main(void)  
{  
  int i;  
  char str[80];  
     
  cout << "I like Turbo C++.\n";  
     
  printf("You can use printf(), but most C++ programs don"t.\n");  
     
  cout << "enter a number: ";  
  cin >> i;  
     
  cout << "your number is " << i << "\n";  
     
  cout << "enter a string: ";  
  cin >> str;  
  cout << str;  
     
  return 0;  
}

Read int and do the calculation

#include <iostream>
using namespace std;
int main()
{  
   cout << "How many pennies do you have? ";
   int count;
   cin >> count;
   double total = count * 0.01;
   cout << "How many nickels do you have? ";
   cin >> count;
   total = count * 0.05 + total;
   cout << "How many dimes do you have? ";
   cin >> count;
   total = count * 0.10 + total;
   cout << "How many quarters do you have? ";
   cin >> count;
   total = count * 0.25 + total;
   
   cout << "Total value = " << total << "\n";
   return 0;
}

Read integer from console using cin

#include <iostream>  
using namespace std;  
  
int main()  
{  
  int length; 
  int width;  
  
  cout << "Enter the length: "; 
  cin >> length; // input the length 
 
  cout << "Enter the width: "; 
  cin >> width;  // input the width  
  
  cout << "The area is ";  
  cout << length * width;
  
  return 0;  
}
Enter the length: 12
Enter the width: 12
The area is 144"

Use relational operators to compare int type variables

#include <iostream>   
using namespace std;   
  
int main(){   
   int numb;   
  
   cout << "Enter a number: ";   
   cin >> numb;   
   cout << "numb<10  is " << (numb < 10)  << endl;   
   cout << "numb>10  is " << (numb > 10)  << endl;   
   cout << "numb==10 is " << (numb == 10) << endl;   
   return 0;   
}