• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 01:25
CEST 07:25
KST 14:25
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
[ASL19] Finals Recap: Standing Tall6HomeStory Cup 27 - Info & Preview18Classic wins Code S Season 2 (2025)16Code S RO4 & Finals Preview: herO, Rogue, Classic, GuMiho0TL Team Map Contest #5: Presented by Monster Energy6
Community News
Flash Announces Hiatus From ASL40Weekly Cups (June 23-29): Reynor in world title form?12FEL Cracov 2025 (July 27) - $8000 live event16Esports World Cup 2025 - Final Player Roster14Weekly Cups (June 16-22): Clem strikes back1
StarCraft 2
General
Statistics for vetoed/disliked maps The SCII GOAT: A statistical Evaluation Weekly Cups (June 23-29): Reynor in world title form? StarCraft Mass Recall: SC1 campaigns on SC2 thread How does the number of casters affect your enjoyment of esports?
Tourneys
RSL: Revival, a new crowdfunded tournament series [GSL 2025] Code S: Season 2 - Semi Finals & Finals $5,100+ SEL Season 2 Championship (SC: Evo) FEL Cracov 2025 (July 27) - $8000 live event HomeStory Cup 27 (June 27-29)
Strategy
How did i lose this ZvP, whats the proper response Simple Questions Simple Answers
Custom Maps
[UMS] Zillion Zerglings
External Content
Mutation # 480 Moths to the Flame Mutation # 479 Worn Out Welcome Mutation # 478 Instant Karma Mutation # 477 Slow and Steady
Brood War
General
[ASL19] Finals Recap: Standing Tall Flash Announces Hiatus From ASL Help: rep cant save BGH Auto Balance -> http://bghmmr.eu/ Where did Hovz go?
Tourneys
[Megathread] Daily Proleagues [BSL20] GosuLeague RO16 - Tue & Wed 20:00+CET The Casual Games of the Week Thread [BSL20] ProLeague LB Final - Saturday 20:00 CET
Strategy
Simple Questions, Simple Answers I am doing this better than progamers do.
Other Games
General Games
Stormgate/Frost Giant Megathread Nintendo Switch Thread Path of Exile What do you want from future RTS games? Beyond All Reason
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread Vanilla Mini Mafia
Community
General
Trading/Investing Thread Things Aren’t Peaceful in Palestine US Politics Mega-thread The Games Industry And ATVI Stop Killing Games - European Citizens Initiative
Fan Clubs
SKT1 Classic Fan Club! Maru Fan Club
Media & Entertainment
Anime Discussion Thread [Manga] One Piece [\m/] Heavy Metal Thread Korean Music Discussion
Sports
2024 - 2025 Football Thread NBA General Discussion Formula 1 Discussion TeamLiquid Health and Fitness Initiative For 2023 NHL Playoffs 2024
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List
Blogs
from making sc maps to makin…
Husyelt
Blog #2
tankgirl
Game Sound vs. Music: The Im…
TrAiDoS
StarCraft improvement
iopq
Heero Yuy & the Tax…
KrillinFromwales
Trip to the Zoo
micronesia
Customize Sidebar...

Website Feedback

Closed Threads



Active: 500 users

C++ revision help!

Blogs > konadora
Post a Reply
1 2 3 4 5 6 7 Next All
konadora *
Profile Blog Joined February 2009
Singapore66156 Posts
Last Edited: 2009-06-28 14:47:08
June 28 2009 14:36 GMT
#1
I want to start off by saying that I'm quite noob with C++ programming, and I need to revise everything in 4 hours' time. Guides online have more terms introduced than explained, so I'm hoping I'll get more luck here.

First, what's the difference between:

int main()


and

void main()


? I keep seeing int main() being used more often in examples, but in school, I've been only using void main().

Second, why is this invalid?

#include <iostream>
using namespace std;

void main()
{
int x;
cout << "Please enter an integer." << endl;
cin >> x >> endl;

cout << "You have entered "x << endl;

}


I get these errors:

1>c:\users\user\documents\visual studio 2008\projects\p1\p1\p1.cpp(10) : error C2146: syntax error : missing ';' before identifier 'x'
1>c:\users\user\documents\visual studio 2008\projects\p1\p1\p1.cpp(10) : error C2563: mismatch in formal parameter list
1>c:\users\user\documents\visual studio 2008\projects\p1\p1\p1.cpp(10) : error C2568: '<<' : unable to resolve function overload


As well as this message while debugging:
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(974): could be 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> & )'
1> with
1> [
1> _Elem=wchar_t,
1> _Traits=std::char_traits<wchar_t>
1> ]
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(966): or 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> & )'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\ostream(940): or 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> & )'


Fixed!

+ Show Spoiler [Fixed ver] +
#include <iostream>
using namespace std;

void main()
{
int x;
cout << "Please enter an integer." << endl;
cin >> x;

cout << "You have entered " << x << endl;

}


I need to revise right from the basics, learning how to do sorts (bubble sorts, insertion sorts, quick sorts, binary search) as well as learning how to use weights, strings etc.

This is one I did quite some time ago, but I didn't revise and completely forgot the reason behind it:

#include <iostream>
#include <string>
using namespace std;

void main() {
char digit[12]= { 'x', 'A', 'B','C','D','E','F','G','H','I','Z','J'};
int weight[8] = { 0,2,7,6,5,4,3,2 };
string nric;
int sum=0; // init cumulative sum to zero

// get NRIC input
cout << "Enter NRIC: ";
getline (cin,nric);
cout << nric << endl;
// calculate check digit
for (int i=1; i<=7; i++) {
sum = sum + (nric[i]-'0')*weight[i];
}
cout << sum << endl;
int checkdigit = 11 - (sum%11);
// check if the user's NRIC check digit matches to confirm valid nric
if (digit[checkdigit]==nric[8])
cout << "valid NRIC number" << endl;
else
cout << "Invalid NRIC number" << endl;
}


Help would be greatly appreciated!!!

POGGERS
Divinek
Profile Blog Joined November 2006
Canada4045 Posts
June 28 2009 14:38 GMT
#2
without ever doing c++ and knowledge from other languages i would say the first question is int and void are return types you know int returns an integer and void doesnt return anything
Never attribute to malice that which can be adequately explained by stupidity.
Oh goodness me, FOX tv where do you get your sight? Can't you keep track, the puck is black. That's why the ice is white.
AoN.DimSum
Profile Blog Joined September 2008
United States2983 Posts
June 28 2009 14:39 GMT
#3
using int function will give the result as a integer, that is why return 0 is used a lot when using int main();
But using void main() gives no result im pretty sure. That is how i understand it. ( I think im sure)
by my idol krokkis : "U better hope Finland wont have WCG next year and that I wont gain shitloads of skill, cause then I will wash ur mouth with soap, little man."
AcrossFiveJulys
Profile Blog Joined September 2005
United States3612 Posts
June 28 2009 14:42 GMT
#4

#include <iostream>
using namespace std;

void main()
{
int x;
cout << "Please enter an integer." << endl;
cin >> x >> endl;

cout << "You have entered "x << endl;

}


you need to put another "<<" before x on the last line. so change the last line to

cout << "You have entered " << x << endl;
konadora *
Profile Blog Joined February 2009
Singapore66156 Posts
June 28 2009 14:42 GMT
#5
I don't really quite understand the concept of 'returning an integer', what does it mean?

Also, if you go for int main(), you must end off with a return 0; , but if you use void main(), you don't have to, right?
POGGERS
konadora *
Profile Blog Joined February 2009
Singapore66156 Posts
June 28 2009 14:44 GMT
#6
Okay I got the short program to work, I foolishly used cin >> x >> endl; instead of cin >> x;

#include <iostream>
using namespace std;

void main()
{
int x;
cout << "Please enter an integer." << endl;
cin >> x;

cout << "You have entered " << x << endl;

}
POGGERS
AcrossFiveJulys
Profile Blog Joined September 2005
United States3612 Posts
June 28 2009 14:47 GMT
#7
On June 28 2009 23:42 konadora wrote:
I don't really quite understand the concept of 'returning an integer', what does it mean?

Also, if you go for int main(), you must end off with a return 0; , but if you use void main(), you don't have to, right?


no. if you make main's return type int, then you can return any integer you want. the point of this is so that a program that calls your main function (such as the program you are using to test it, like a terminal or visual studio) can retrieve the return value. you can use this communication to return error codes... i.e., return 0 if all goes well, or return > 0 if an error occurred.

if you make main void, then it has no return type, and will always return 0 to the caller.
konadora *
Profile Blog Joined February 2009
Singapore66156 Posts
June 28 2009 14:49 GMT
#8
Oh I see, so it's just that int main() allows you to identify whether your program has an error or not, but void main() can't?
POGGERS
Revabug
Profile Blog Joined June 2009
United Kingdom147 Posts
June 28 2009 15:01 GMT
#9
void main() should never be used as the main function should ALWAYS return a value

I thought everyone that?!
konadora *
Profile Blog Joined February 2009
Singapore66156 Posts
June 28 2009 15:05 GMT
#10
On June 29 2009 00:01 Revabug wrote:
void main() should never be used as the main function should ALWAYS return a value

I thought everyone that?!

Would you mind explaining a bit more? I already stated that I'm quite noob with C++
POGGERS
Tracil
Profile Blog Joined September 2005
Australia505 Posts
Last Edited: 2009-06-28 15:07:37
June 28 2009 15:06 GMT
#11
When you're doing a small project, as is typical for a lot of uni work, what your main function returns basically won't matter. Albeit obviously it's better practice to use int, and bad habits are bad.
Shooting
sidesprang
Profile Joined January 2009
Norway1033 Posts
June 28 2009 15:06 GMT
#12
never use void main. Its oldschool bad programming.
Float like a butterfly, sting like a bee.
konadora *
Profile Blog Joined February 2009
Singapore66156 Posts
Last Edited: 2009-06-28 15:09:06
June 28 2009 15:08 GMT
#13
Okay, so I've got that cleared. Now, I need to tackle all these 'floats', 'doubles', 'integers', etc. What are the names of these? Are they the 'return data types'?
POGGERS
treason
Profile Joined February 2009
Germany72 Posts
Last Edited: 2009-06-28 15:11:01
June 28 2009 15:09 GMT
#14
On June 28 2009 23:36 konadora wrote:
Show nested quote +
#include <iostream>
#include <string>
using namespace std;

void main() {
char digit[12]= { 'x', 'A', 'B','C','D','E','F','G','H','I','Z','J'};
int weight[8] = { 0,2,7,6,5,4,3,2 };
string nric;
int sum=0; // init cumulative sum to zero

// get NRIC input
cout << "Enter NRIC: ";
getline (cin,nric);
cout << nric << endl;
// calculate check digit
for (int i=1; i<=7; i++) {
sum = sum + (nric[i]-'0')*weight[i];
}
cout << sum << endl;
int checkdigit = 11 - (sum%11);
// check if the user's NRIC check digit matches to confirm valid nric
if (digit[checkdigit]==nric[8])
cout << "valid NRIC number" << endl;
else
cout << "Invalid NRIC number" << endl;
}


char digit[12]= { 'x', 'A', 'B','C','D','E','F','G','H','I','Z','J'};
creates an array of characters with the length of 12, digit[0] == 'x', digit[1] == 'A', ... , digit[11] == 'J'
int weight[8] = { 0,2,7,6,5,4,3,2 };
same with integer values
cout << "Enter NRIC: ";
getline (cin,nric);

here you get an input from the user, he types something on his keyboard, and the input is saved in the string (string = array of char) nric.
for (int i=1; i<=7; i++) {
sum = sum + (nric[i]-'0' )*weight[i];
}

this for-loop iterates through the array the user has typed. Its strange, that you dont start with i = 0, since arrays in C, Java and C++ are beginning with the index 0.
Superbia
Profile Blog Joined April 2008
Netherlands8889 Posts
June 28 2009 15:10 GMT
#15
The only thing int main does is use the int you return as a parameter for ExitProcess (or exit, which is basically a wrapper for ExitProcess). void main will always return 0.
Minimal effort.
konadora *
Profile Blog Joined February 2009
Singapore66156 Posts
June 28 2009 15:13 GMT
#16
On June 29 2009 00:09 treason wrote:
Show nested quote +
On June 28 2009 23:36 konadora wrote:
#include <iostream>
#include <string>
using namespace std;

void main() {
char digit[12]= { 'x', 'A', 'B','C','D','E','F','G','H','I','Z','J'};
int weight[8] = { 0,2,7,6,5,4,3,2 };
string nric;
int sum=0; // init cumulative sum to zero

// get NRIC input
cout << "Enter NRIC: ";
getline (cin,nric);
cout << nric << endl;
// calculate check digit
for (int i=1; i<=7; i++) {
sum = sum + (nric[i]-'0')*weight[i];
}
cout << sum << endl;
int checkdigit = 11 - (sum%11);
// check if the user's NRIC check digit matches to confirm valid nric
if (digit[checkdigit]==nric[8])
cout << "valid NRIC number" << endl;
else
cout << "Invalid NRIC number" << endl;
}


char digit[12]= { 'x', 'A', 'B','C','D','E','F','G','H','I','Z','J'};
creates an array of characters with the length of 12, digit[0] == 'x', digit[1] == 'A', ... , digit[11] == 'J'
int weight[8] = { 0,2,7,6,5,4,3,2 };
same with integer values
cout << "Enter NRIC: ";
getline (cin,nric);

here you get an input from the user, he types something on his keyboard, and the input is saved in the string (string = array of char) nric.
for (int i=1; i<=7; i++) {
sum = sum + (nric[i]-'0' )*weight[i];
}

this for-loop iterates through the array the user has typed. Its strange, that you dont start with i = 0, since arrays in C, Java and C++ are beginning with the index 0.

I need help with explanation on the 'arrays' part, I can't find the notes online on my school's e-learning platform. Step-by-step explanation would help so much, I would love you for life
POGGERS
tec27
Profile Blog Joined June 2004
United States3696 Posts
June 28 2009 15:15 GMT
#17
On June 28 2009 23:47 AcrossFiveJulys wrote:
Show nested quote +
On June 28 2009 23:42 konadora wrote:
I don't really quite understand the concept of 'returning an integer', what does it mean?

Also, if you go for int main(), you must end off with a return 0; , but if you use void main(), you don't have to, right?


no. if you make main's return type int, then you can return any integer you want. the point of this is so that a program that calls your main function (such as the program you are using to test it, like a terminal or visual studio) can retrieve the return value. you can use this communication to return error codes... i.e., return 0 if all goes well, or return > 0 if an error occurred.

if you make main void, then it has no return type, and will always return 0 to the caller.

Just to be clear here, making main type void makes it return *no* value which is not at all the same as returning 0. At a lower level, the return value of a function is usually stored in a register, therefore, if the operating system (which in this case acts as a 'calling function' expects main to be of type int and calls it that way, it will receive an effectively random value in return if it is type void. As you might imagine, thats a bad thing that just happens to turn out okay sometimes. In short, make your main function type int.
Can you jam with the console cowboys in cyberspace?
Superbia
Profile Blog Joined April 2008
Netherlands8889 Posts
June 28 2009 15:16 GMT
#18
On June 29 2009 00:15 tec27 wrote:
Show nested quote +
On June 28 2009 23:47 AcrossFiveJulys wrote:
On June 28 2009 23:42 konadora wrote:
I don't really quite understand the concept of 'returning an integer', what does it mean?

Also, if you go for int main(), you must end off with a return 0; , but if you use void main(), you don't have to, right?


no. if you make main's return type int, then you can return any integer you want. the point of this is so that a program that calls your main function (such as the program you are using to test it, like a terminal or visual studio) can retrieve the return value. you can use this communication to return error codes... i.e., return 0 if all goes well, or return > 0 if an error occurred.

if you make main void, then it has no return type, and will always return 0 to the caller.

Just to be clear here, making main type void makes it return *no* value which is not at all the same as returning 0. At a lower level, the return value of a function is usually stored in a register, therefore, if the operating system (which in this case acts as a 'calling function' expects main to be of type int and calls it that way, it will receive an effectively random value in return if it is type void. As you might imagine, thats a bad thing that just happens to turn out okay sometimes. In short, make your main function type int.


You're wrong, the compiler automatically makes the main function return 0 when it's specified as void. (xor eax, eax before ret)
Minimal effort.
Revabug
Profile Blog Joined June 2009
United Kingdom147 Posts
Last Edited: 2009-06-28 15:20:57
June 28 2009 15:17 GMT
#19
oops
AcrossFiveJulys
Profile Blog Joined September 2005
United States3612 Posts
June 28 2009 15:20 GMT
#20
On June 29 2009 00:08 konadora wrote:
Okay, so I've got that cleared. Now, I need to tackle all these 'floats', 'doubles', 'integers', etc. What are the names of these? Are they the 'return data types'?


you really need to read your textbook. float, double, int, are all variable types. this is a very basic concept, and you won't get far asking such questions here.
1 2 3 4 5 6 7 Next All
Please log in or register to reply.
Live Events Refresh
Next event in 4h 35m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Nina 227
StarCraft: Brood War
PianO 302
Snow 118
JulyZerg 78
Aegong 67
Nal_rA 48
Rock 35
ajuk12(nOOB) 14
Noble 13
Bale 11
Icarus 7
Dota 2
NeuroSwarm108
League of Legends
JimRising 763
Counter-Strike
Stewie2K800
Super Smash Bros
Mew2King234
amsayoshi43
Heroes of the Storm
Khaldor104
Other Games
summit1g9128
shahzam843
WinterStarcraft363
hungrybox351
RuFF_SC276
Organizations
Other Games
gamesdonequick975
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 16 non-featured ]
StarCraft 2
• Berry_CruncH299
• practicex 48
• OhrlRock 4
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Rush1224
• Lourlo978
• Stunt392
Upcoming Events
The PondCast
4h 35m
RSL Revival
4h 35m
ByuN vs Classic
Clem vs Cham
WardiTV European League
10h 35m
Replay Cast
18h 35m
RSL Revival
1d 4h
herO vs SHIN
Reynor vs Cure
WardiTV European League
1d 10h
FEL
1d 10h
Korean StarCraft League
1d 21h
CranKy Ducklings
2 days
RSL Revival
2 days
[ Show More ]
FEL
2 days
Sparkling Tuna Cup
3 days
RSL Revival
3 days
FEL
3 days
BSL: ProLeague
3 days
Dewalt vs Bonyth
Replay Cast
4 days
Replay Cast
5 days
The PondCast
6 days
Replay Cast
6 days
Liquipedia Results

Completed

Proleague 2025-06-28
HSC XXVII
Heroes 10 EU

Ongoing

JPL Season 2
BSL 2v2 Season 3
BSL Season 20
Acropolis #3
KCM Race Survival 2025 Season 2
CSL 17: 2025 SUMMER
Copa Latinoamericana 4
Championship of Russia 2025
RSL Revival: Season 1
Murky Cup #2
BLAST.tv Austin Major 2025
ESL Impact League Season 7
IEM Dallas 2025
PGL Astana 2025
Asian Champions League '25
BLAST Rivals Spring 2025
MESA Nomadic Masters
CCT Season 2 Global Finals
IEM Melbourne 2025
YaLLa Compass Qatar 2025

Upcoming

CSLPRO Last Chance 2025
CSLPRO Chat StarLAN 3
K-Championship
uThermal 2v2 Main Event
SEL Season 2 Championship
FEL Cracov 2025
Esports World Cup 2025
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
BLAST Open Fall Qual
Esports World Cup 2025
BLAST Bounty Fall 2025
BLAST Bounty Fall Qual
IEM Cologne 2025
FISSURE Playground #1
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2025 TLnet. All Rights Reserved.