c ++ _ cost day1_cin

Write a program, it requires the user to enter a string of integers, and any number of spaces, which must be an integer in the same row, but allows appear anywhere in the line. When the user presses the enter key, the input data is stopped. The program automatically sum of all integers and print out the results.

The need to address two issues, digital extraction, extraction consecutive numbers.

C // 
#include <stdio.h> 

void main () 
{ 
	int SUM = 0 ;; 
	int I; 
	char CH; 

	the while (Scanf ( "% D", & I) ==. 1) 
	{ 
		SUM + = I; 
		
		the while (( ch = getchar ()) == ' ') // shielded space 
			; 

		IF (ch == '\ n-') 
			BREAK; 

		ungetc (ch, stdin); // return the stored character variable ch input to stdin stream. 

	} 

	The printf ( "SUM;% D \ n-", SUM); 
}

  

//c++
#include<iostream>

using namespace std;

int main()
{
	int sum=0;

	cout<<"请输入";
	 
	int i;
	
	while(cin>>i)
	{
		sum+=i;
	
		while(cin.peek()==' ')
		{
			cin.get();
		}
		if(cin.peek()=='\n')
			break;
	}

	cout<<"sum:"<<sum<<endl;

	return 0;

}

① expression cin >> i returns the object itself that is input stream cin, but this end of the file to read or extract if the operator encounters an illegal value, the return value is false.

#include<iostream>

using namespace std;

int main()
{
	char buf[20];
	
	cin.ignore(7);
	cin.getline(buf,10);
	
	cout<<buf<<endl;
	
	return 0;
}
/*
输入:12345 12345 12345 12345
输出:2345 1234
*/ 

②cin.ignore (7) ignored the first seven characters

③cin.getline (buff, 10) stored in buf to obtain ten (the tenth bit is a '\ 0)

 ④using namespace std; namespace, c ++ identifiers are all in the same special namespace (std) defined. If you do not use this instruction, we need to use this syntax to call std :: cout output stream object.

#include <the iostream> 

the using namespace STD; 

int main () 
{ 
	const int SIZE = 50; 
	char buf [SIZE]; 
	
	COUT << "Please enter a text:"; 
	cin.read (buf, 20 is); 
	
	COUT << " number of characters in the string is collected: " 
		<< cin.gcount () << endl; 
	
	COUT <<" information is input text: "; 
	cout.write (buf, 20 is); 
	COUT << endl; 
	
	return 0; 
 
}

  

Guess you like

Origin www.cnblogs.com/KIROsola/p/12154528.html