C++ Learning Case

Keywords: ascii Programming encoding Windows

C++ is a good programming language. At the same time, C++ is a rugged road. I want to witness my growth here with you. I will share some cases, source code and learning experience with you from time to time every day.

Case 1: Title Description: Enter a string to determine how many strings there are, with spaces as the boundary.

#include "stdafx.h"
#include<iostream>


int _tmain(int argc, _TCHAR* argv[])
{
	//This is a small procedure to determine how many words there are in each line.
	int i,num = 0, word = 0;
	char string[81];
	char c;
	std::cout << "please input the word :" ;
	
	gets_s(string);
	for (i= 0; (c = string[i]) != 0; i++)
		if (c == ' ')
			word = 0;
		else if (word ==0){
			word = 1;
			num++;
		}
		std::cout << "this is %d word in this line\n", num;
		system("pause");
	return 0;
}

Note: The difference between _tmain() and main() here is as follows:

main() is the function entry of standard C++. Standard C++ program entry point function, default character encoding format ANSI

The function signature is:

int main();

int main(int argc, char* argv[]);

_ tmain() is a program entry point function provided by windows for automatic conversion of unicode character set and ANSI character set

The function signature is:

int _tmain(int argc, TCHAR *argv[])

(1) When your program's current character set is unicode, int_tmain (int argc, TCHAR * argv []) is translated into

int wmain(int argc, wchar_t *argv[])

// wmain is also another alias of main to support a two-byte language environment

(2) The get () function retrieves a string of input strings from the console

Case 2: By writing a function to calculate the array, the sum of the numbers in the array is calculated by calling the calculation function in the main function.

#include "stdafx.h"
#include<iostream>

//Define a function that calculates the sum of numbers in an array
int add_list(int list[], int lenght){
	int sum = 0;
	for (int i=0; i < lenght; i++){
		sum += list[i];
	}
	return sum;
}

int _tmain(int argc, _TCHAR* argv[])
{
	//Call functions to count the sum of numbers in an array
	int list[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9,10 };
	int lenght = sizeof(list) / sizeof(list[0]);
	int sum_total=add_list(list, lenght);
	std::cout << "The sum of the calculations is:" << sum_total;
	system("pause");
	return 0;
}

Note: It's important to calculate the length of the array here, because you need to pass the termination condition of the incoming loop to the calling function.

The function of sizeof() is to return the number of bytes in memory occupied by an object or type. By dividing the total number of bytes in the list by the number of bytes in the first element of the list, the number of elements in the character array can be obtained.

Case 3: Write a program that asks the user to input any integer and space number. When the user presses ENTER key, the program automatically sums up and prints the result.

// Console Application 1. cpp: Defines the entry point for console applications.
//

#include "stdafx.h"
#include<iostream>

//Define a function that calculates the sum of numbers in an array
int add_list(int list[], int lenght){
	int sum = 0;
	for (int i=0; i < lenght; i++){
		sum += list[i];
	}
	return sum;
}

int _tmain(int argc, _TCHAR* argv[])
{
	//Call functions to count the sum of numbers in an array
	std::cout << "Please enter any integer and space number, press Enter key to sum statistics after input." << std::endl;
	char string[80];
	char c;
	gets_s(string);
	int sum = 0;
	for (int i = 0; (c = (int)string[i]) != 0; i++){

		if (c == ' ')
			continue;
		else
			sum += c - '0';

	}
	
	std::cout << "The sum of the calculations is:" << sum;
	system("pause");
	return 0;
}

Note: The get_s() function is also used to accept the string input from the console, where the character c is the ASCII type of the console number, for example, sum+='1', which corresponds to the ASCII value of 1, so the value of sum is 49, so we use sum += C -'0'to get the input integer value.

Method 2: peek() and get() methods for input stream classes and input stream classes

#include "stdafx.h"
#include<iostream>

int _tmain(int argc, _TCHAR* argv[])
{
	//Implementing this function using input and output streams
	std::cout << "Please enter any integer and space number, press Enter key to sum statistics after input." << std::endl;
	int i, sum = 0;
		while (std::cin >> i ){
			sum += i;
			while (std::cin.peek() == ' '){
				std::cin.get();
			}
			if (std::cin.peek() == '\n'){
				break;
			}
		}
		std::cout << "The sum of the calculations is:" << sum;
	system("pause");
	return 0;
}

Note: peek() here is to read whether the next character is a space. If it is a space, get() function is used to remove the space. When encountering Enter, the program automatically ends.

Posted by illusiveone on Sat, 21 Sep 2019 01:18:42 -0700