C++ tic-tac-toe

Pages: 12
How can you fix this?
I can only use an online website, cpp.sh, so I need help.
Trying to make one for all elements

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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//perodic table of elements
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
void introduction();
void elementlist();
char askYesNo(string question);
string element_name_entered;
int main()
{
    cout<<"\nThis is always displayed.";
    cout<<"\nThis is the introduction screen for the Elements.";
    intro:
    cout<<"\nThere are three places you can go to,";
    cout<<"\nThey are: 1=element list; 2=element enter, and 3=quit";
    int direction;
    cin>>direction;
    if (direction==1)
    {
        cout<<"\nThis is a program to find out about elements";
        cout<<"\nThe elements avalible are: ";
        cout<<"\nhydrogen, helium, lithium, beryllium, boron, carbon, nitrogen, oxygen, flourine, neon, sodium, magnesium, aluminum, silicon, ";
        cout<<"\nphosphorus, sulfer, chlorine, argon, potassium, calcium, scandium, titanium, vandium, chromium, manganese, iron, cobalt, nickel, copper, ";
        cout<<"\nzinc, gallium, germanium, arsinic, selenium, bromine, krypton, rubidium, strontanium, yttrium, zirconium, niobium, molybdenum, ";
        cout<<"\ntechnetium, ruthenium, rhodium, palladium, silver, cadmium, indium, tin, antimony, tellurium, iodine, xenon, cesium, ";
        cout<<"\nbarium, lanthanum, cerium, praseodymium, neodymium, promethium, samarium, europium, gadolinium, terbium, dysprosium ";
        cout<<"\nholmium, erbium, thulium, ytterbium, lutetium, hafnium, tantilum, tungsten, rhenium, osmium, iridium, platnium, gold, mercury";
        cout<<"\nlead, bismuth, polonium, astitine, radon, francium, radium, actnium, thorium, protactinium, uranium, neptunium, plutonium, americium, ";
        cout<<"\ncurium,berkelium, californium, einsteinium, fermium, mendelevium, nobelium, lawrencium, rutherfordium, dubnium, seaborgium, bohrium, ";
        cout<<"\nhassium, meitnerium, darmstadtium, roentgenium, copernicium, ununtrium, ununquadium, ununpentium, ununhexium, ununsexium, ununoctium.";
        cout <<"\n\t\t\t\tThese are 118 of the elements";
        cout<<"...All of the known ones.\n";
        goto intro;
    }
    else if(direction==2)
    { 
        cout<<"\n Enter the element name correctly.";
        cin>>element_name_entered;
        goto element_list_of_all;
    }
    else if (direction==3)
    {
        question_ask:
        cout<<"\n Are you sure(yes(1)/no(2))";
        bool quitq;
        cin>>quitq;
        if (quitq==1)
        {
            cout<<"\n......Transferring to end screen......";
            goto endL;
        }
        else if (quitq==0)
        {
            goto intro;
        }
        else
        {
            cout<<"\nInvalid answer .going to question ask.";
            goto question_ask;
        }
    }
    else
    {
        cout<<"\nInvalid answer";
        
    }
    endL:
    cout<<"\nProgram end";
    return 0;
}
void elementlist()
{
    element_list_of_all:
    cout<<"Displaying element facts";
    if (element_name_entered==hydrogen)
    {
        cout<<"\nHydrogen";
        cout<<"\nFacts Atomic number:1	Atomic weight:1.0079";
        cout<<"\nSymbol:H  Electron config:1s1";
        cout<<"\nMelting point:-259  Boiling point:-253";
        cout<<"\nEnd of Hydrogen. "; 
    }
    else if (element_name_entered==helium)
    {
        cout<<"\n Adding.";
    }
    else if (element_name_entered==lithium)
    {
        cout<<"\n Adding.";
    }
    else if (element_name_entered==)// do for each
    {
        cout<<"\n Adding.";
    }
    else if (element_name_entered==)// do for each
    {
        cout<<"\n Adding.";
    }
    else if (element_name_entered==)// do for each
    {
        cout<<"\n Adding.";
    }
    else if (element_name_entered==)// do for each
    {
        cout<<"\n Adding.";
    }
    else if (element_name_entered==)// do for each
    {
        cout<<"\n Adding.";
    }
    else if (element_name_entered==)// do for each
    {
        cout<<"\n Adding.";
    }
    else
    {
        cout<<"\n Adding.";//or
        cout<<"\n Invalid answer.";
    }
    goto intro;
    
}
Last edited on
This is for a science class so please help me
What exactly do you want your code to do?
Hi,

The first big things you can do, are to not use goto - use functions and loops more.

You posted 2 main functions. You can't have that, or was it a copy paste error?

It might help to write down in English what you want to do, like writing a recipe. start out very simple, then keep going back and refine and add more detail, until you are happy to convert it to code.

This process is called doing pseudo code, and is surprisingly valuable. You can identify where loops need to be, what needs to be in a function, think about what types yo are going to use.

It might sound as boring as hell, but I think it could be very useful for you.

Cheers
Definetely a good suggestion from TheIdeasMan. After you finish your pseudo code, you might want to have a look at the "Tutorials" section of the site, in the top left side of the page. If you can spend a little time, it'll be a good way to learn the basics in C++.
closed account (E3h7X9L8)
this is not tic tac toe...
This is clearly not Tic-Tac-Toe,

how ever it looks like what this code is meant to do is display the name, atomic number, atomic weight, elemental symbol, and the electron configuration of the entered element yes?

well for something like that I would use arrays,
array of strings for element name,
array of integers for atomic number,
array of floats for atomic weight,
array of strings for elemental symbol,
array of strings for electron configuration.

once you have all of those filled out you can use a loop and compare the input to strings in the element name array, and then save the array key for the matching string and use it to pull the rest of the data from the other arrays, and if a match isn't made just have it exit with a message saying unknown element or something.

and if you wanted the list to be changeable without recompiling the program, you could enter the data in a text file and have the program read file for the comparing and just grab the matching line.

if this is the type of thing you are after I can go ahead and give some actual code to help you get started.
Sorry about the placement.....
I will remove second int main
Done
To youare29 , can you please give me some code for it... I need it done ASAP
We don't write peoples' homework for them here. We'll give you advice, explain things, and help find problems, but you'll have to write the code yourself.

Have a go at it. When you hit a problem, come back, show us your code and explain clearly and precisely what the problem is, and we'll do what we can to help.
Ok
:)
closed account (48T7M4Gy)
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
#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;
using std::cin;

int main()
{
	string element[] = {
		"hydrogen","helium","lithium","beryllium","boron","carbon","nitrogen","oxygen",
		"flourine","neon","sodium","magnesium","aluminum","silicon","phosphorus","sulfer",
		"chlorine","argon","potassium","calcium","scandium","titanium","vandium","chromium",
		"manganese","iron","cobalt","nickel","copper","zinc","gallium","germanium",
		"arsinic","selenium","bromine","krypton","rubidium","strontanium","yttrium",
		"zirconium","niobium","molybdenum","technetium","ruthenium","rhodium",
		"palladium","silver","cadmium","indium","tin","antimony","tellurium","iodine",
		"xenon","cesium","barium","lanthanum","cerium","praseodymium","neodymium","promethium",
		"samarium","europium","gadolinium","terbium","dysprosium ","holmium","erbium",
		"thulium","ytterbium","lutetium","hafnium","tantilum","tungsten","rhenium",
		"osmium","iridium","platnium","gold","mercury","lead","bismuth","polonium",
		"astitine","radon","francium","radium","actnium","thorium","protactinium",
		"uranium","neptunium","plutonium","americium","curium,berkelium","californium",
		"einsteinium","fermium","mendelevium","nobelium","lawrencium","rutherfordium",
		"dubnium","seaborgium","bohrium","hassium", "meitnerium", "darmstadtium",
		"roentgenium", "copernicium", "ununtrium", "ununquadium", "ununpentium",
		"ununhexium", "ununsexium", "ununoctium" };

	int noOfElements = sizeof(element) / sizeof(string);
	bool found = false;
	string searchElement = "";
	int index = 0;

	// LIST OUT ALL ELEMENTS
	cout << "There are " << noOfElements << " elements and they are: " << endl;
	for (int i = 0; i < noOfElements; i++)
		cout << i << '\t' << element[i] << endl;

	// SEARCH FOR AN ELEMENT
	cout << "What element are you looking for? ";
	cin >> searchElement;

	for (int i = 0; i < noOfElements; i++)
	{
		if (element[i] == searchElement)
		{
			found = true;
			index = i;
		}
	}

	if (found)
		cout << searchElement << " is element no. " << index << endl;
	else
		cout << "Element not found." << endl;
	
	system("pause");
	return 0;
}


http://www.ptable.com/
Last edited on
Thank you!!!! This will give me some help.
closed account (48T7M4Gy)
Yeah, there are lots of opportunities, with more arrays with properties, electrons and shell structure. The list goes on.

Let us know how you get on or if you have any problems. :)
based on that you should try to make it more easy to use and try to add the ability to search for it by it's atomic #. I tweaked the code that kemort just posted with this:
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
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;

int main()
{
	string element[] = {
		"hydrogen","helium","lithium","beryllium","boron","carbon","nitrogen","oxygen",
		"flourine","neon","sodium","magnesium","aluminum","silicon","phosphorus","sulfer",
		"chlorine","argon","potassium","calcium","scandium","titanium","vandium","chromium",
		"manganese","iron","cobalt","nickel","copper","zinc","gallium","germanium",
		"arsinic","selenium","bromine","krypton","rubidium","strontanium","yttrium",
		"zirconium","niobium","molybdenum","technetium","ruthenium","rhodium",
		"palladium","silver","cadmium","indium","tin","antimony","tellurium","iodine",
		"xenon","cesium","barium","lanthanum","cerium","praseodymium","neodymium","promethium",
		"samarium","europium","gadolinium","terbium","dysprosium ","holmium","erbium",
		"thulium","ytterbium","lutetium","hafnium","tantilum","tungsten","rhenium",
		"osmium","iridium","platnium","gold","mercury","lead","bismuth","polonium",
		"astitine","radon","francium","radium","actnium","thorium","protactinium",
		"uranium","neptunium","plutonium","americium","curium,berkelium","californium",
		"einsteinium","fermium","mendelevium","nobelium","lawrencium","rutherfordium",
		"dubnium","seaborgium","bohrium","hassium", "meitnerium", "darmstadtium",
		"roentgenium", "copernicium", "ununtrium", "ununquadium", "ununpentium",
		"ununhexium", "ununsexium", "ununoctium" };

	int noOfElements = sizeof(element) / sizeof(string);
	bool found = false;
	string searchElement = "";
	int index=0, protons=0, selection, loadup=0;
    char YorN='Y';
	/// LIST OUT ALL ELEMENTS
    while(loadup<3){
    system("CLS");
    cout<<"Cataloging Elements\n\nLoading";
    Sleep(500);
    system("CLS");
    cout<<"Cataloging Elements\n\nLoading.";
    Sleep(500);
    system("CLS");
    cout<<"Cataloging Elements\n\nLoading..";
    Sleep(500);
    system("CLS");
    cout<<"Cataloging Elements\n\nLoading...";
    Sleep(500);
    system("CLS");
    loadup++;
    }
    system("CLS");
	cout << "There are " << noOfElements-1<< endl;
while(YorN=='Y'||YorN=='y'){
    cout<<"How do you want to search for your Element?\n1. Atomic #\n2. Exact Name\n> ";
    cin>>selection;
    system("CLS");
    if(selection==1){
    /// SEARCH FOR AN ELEMENT BY ATOMIC #
    cout<<"What is the atomic number that your looking for?\n> ";
    cin>>protons; system("CLS");
    cout<<"element #"<<protons<<" is "<<element[protons]<<endl;
    }else if(selection==2){
    /// SEARCH FOR AN ELEMENT BY NAME
	cout << "What is the name of the element that you are looking for? *Case Sensitive*\nYou can search by the atomic # or by the name.\n> ";
	cin >> searchElement;
	for (int i = 0; i < noOfElements; i++)
	{
		if (element[i] == searchElement)
		{
			found = true;
			index = i;
		}
	}
	if (found)
		cout << searchElement << " is element no. " << index << endl;
	else
		cout << "Element not found." << endl;
    }else{
    cout<<"Sorry this program doesn't support that answer."<<endl;
    }

	system("pause");
	system("CLS");
cout<<"Restart?\n Y or N?\n> ";
cin>>YorN;
system("CLS");
}
	return 0;
}
Last edited on
I am working on a similar but more complex program for the balancing of chemical equations.
closed account (48T7M4Gy)
I am working on a similar but more complex program for the balancing of chemical equations.
Cool, coe!
on cpp.sh that does not work
Thanks for the replies
Pages: 12