forked from Taliessin/CPP_with_Qt_Tutorial_code_collection
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·95 lines (82 loc) · 2.73 KB
/
main.cpp
File metadata and controls
executable file
·95 lines (82 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//----------------------------------------------------
// Learn C++ with Qt Tutorial:
// Console output and input
//
// (c) 2014, www.alternative-computer-programming.com
//----------------------------------------------------
// Object class and function library includes
#include <QCoreApplication>
#include <stdio.h>
#include <iostream>
#include <string>
#include <sstream>
// Used object namespaces
using namespace std;
// Main program
int main(int argc, char *argv[])
{
//--- Initializations ---
QCoreApplication a(argc, argv);
char myoutput [60];
int stringlength,
x = 4,
y = 10,
c1 = 'A',
c2 = 'B',
c3 = 'C',
nl = '\n';
string username,
nickname;
char favchar1,
favchar2,
favchar3;
string myinput;
float price = 0;
int amount = 0;
//--- Output demonstration ---
// string output with C function
printf("Hello, World!\n");
// string output with C++ stream functions
cout << "Hello Universe!" << endl << endl;
// formatted output
stringlength = sprintf(myoutput, "%d plus %d equals %d", x, y, x+y);
printf ("[%s] is a string that is %d characters long.\n\n", myoutput, stringlength);
// single character output
putc(c1, stdout);
putc(c2, stdout);
putc(c3, stdout);
putc(nl, stdout);
//--- Input demonstration ---
// string input
cout << endl << "Please enter your username: ";
getline(cin, username);
cout << "Hello " << username << endl;
cout << "What is your nickname? ";
cin >> nickname;
cout << endl << "So, " << username << " is called " << nickname << "?" << endl;
cout << "Ok..." << endl;
// single character input
cout << endl << "What is your favorite character? ";
cin >> favchar1;
fflush(stdin);
cout << "And what is your second favorite character? ";
favchar2 = getc(stdin);
fflush(stdin);
cout << "And the third favorite character? ";
favchar3 = getchar();
cout << endl << "Your favorite characters are " << favchar1;
cout << ", " << favchar2 << " and " << favchar3 << endl;
// stringstream example
fflush(stdin);
cout << endl << "Sell your apples..." << endl;
cout << "Please enter your price per apple: ";
getline(cin, myinput);
stringstream(myinput) >> price;
cout << "How many apples do you want to sell? ";
getline(cin, myinput);
stringstream(myinput) >> amount;
cout << "If you sell all of them, you will earn: " << price*amount << endl;
// end of application
cout << endl << "That's it!" << endl;
return a.exec();
}