Basic array

Pages: 12
I am very new to programming c++ so bare with me.

I was making something for a friend for his game and well it works obviously but I was wondering if somebody could direct me in a more efficient pathway? And explain how it is more efficient.


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
#include <iostream>
#include <string>
#include <windows.h>
#include <string.h>
#include <tchar.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;


int dharok()
{

     int dharok[] = {4716, 4717, 4718, 4719, 4720, 4721, 4722, 4723};
       	cout << "Dharok's Helm: " << dharok[0] << " - Noted: "<< dharok[1] << endl;
       	    	cout << "Dharok's GreatAxe: " << dharok[2] << " - Noted: "<< dharok[3] << endl;
       	    	    	cout << "Dharok's PlateBody: " << dharok[4] << " - Noted: "<< dharok[5] << endl;
       	    	    	    	cout << "Dharok's PlateLegs: " << dharok[6] << " - Noted: "<< dharok[7] << endl;


}


int god()
{

     int Godswords[] = {11694, 11695, 11696, 11697, 11698, 11699, 11670, 11671};
       	cout << "Armadyl Godsword: " << Godswords[0] << " -  Noted:" << Godswords[1] << endl;
       	cout << "Bandos Godsword: " << Godswords[2] << " - Noted:" << Godswords[3] << endl;
       	cout << "Zamorak Godsword: " << Godswords[4] << " -  Noted:" << Godswords[5] << endl;
       	cout << "Saradomin Godsword: " << Godswords[6] << " -  Noted:" << Godswords[7] << endl;



}


int main()
{

string ids;

  cout<<"Search for your item(only currently supports Godswords LOL).\n";
  cin>> ids;
  cin.ignore();

	if (ids == "dharok") {
	    dharok();
	    cin.get();
	    system("CLS");
	      main();
	}
	else if (ids == "greataxe") {
	dharok();
	cin.get();
		    system("CLS");
	      main();
	}

	else if (ids == "godsword") {
	    god();
	    cin.get();
	    	    system("CLS");
	      main();

	}
	else if (ids == "god") {
	    god();
	    cin.get();
	    	    system("CLS");
	      main();

	}
	else if (ids == "armadyl") {
	    god();
	    cin.get();
	    	    system("CLS");
	      main();

	}
	else if (ids == "bandos") {
	    god();
	    cin.get();
	    	    system("CLS");
	      main();

	}
	else if (ids == "zamorak") {
	    god();
	    cin.get();
	    	    system("CLS");
	      main();

	}
	else if (ids == "saradomin") {
	    god();
	    cin.get();
	    	    system("CLS");
	      main();

	}

  cin.get();
  main();
}
Last edited on
You could use switch statement for the if's.

http://www.cplusplus.com/doc/tutorial/control/

Could you give me and example using my code? Thanks :)
Using switch statements aren't really more efficient (although they're not in anyway deficient to if's and else if's) but it's MUCH tidier and MUCH easier for the programmer to see what's happening.

Although i notice that the contents of all your if statement blocks are all the same...
What's the point of having all the if's and else if's if they're all the same?
(perhaps you explain what you want to do and i could help you out with a few suggestions)
Last edited on
Well, I want either certain keywords to show the lists like "int dharok()"
or it has to spell it out..mm lemme explain further. If it is "dh" it will goto dharok()
and if its "dha" it will do the same as if it was just "dharok" but if it was lets say dhe then it would bring back "Nothing Found". If that isn't specific enough tell me :) thanks.
Let me rephrase that, what I mean is it's supposed to be sort of search engine and typing in dh ect, will show all the names starting with dh and then it's id beside it.
closed account (zb0S216C)
Since you're new to programming, recommending optimisations can be difficult. If you can understand your code fully (by knowing what's going on), you'll be able to figure out what needs changing.

The most obvious fix, without confusing you, if the stream of else ifs. Since all else ifs beyond the first else if are the same, you can place them into an else block:

1
2
3
4
5
6
7
8
9
10
11
if((ids == "dharok") || (ids == "greataxe")) 
{
    dharok();
    cin.get();
}

else
{
    god();
    cin.get();
}

Don't call main(); ever.

SatsumaBenji wrote:
"Using switch statements aren't really more efficient (although they're not in anyway deficient to if's and else if's"

I believe switch is more efficient in most cases. Since switch case-labels must be constant integral expressions, the compiler will be able to optimise them, possibly by placing them in ROM. Because if statements handle all types, it would be difficult for the compiler to optimise each type it tests effectively.

Wazzak
Last edited on
I was using something similar or exactly like that previously but it would just go past it if you type anything not specifically "dharok"
I was thinking something similar to this search function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
int main()
{
std::cout << "Enter a String: ";
std::string s;
getline(std::cin, s);
std::cout << "Enter word to be searched: ";
std::string w;
getline(std::cin, w);

bool flag = s.find(w) != std::string::npos;
if (flag) std::cout << "success\n";
else std::cout << "unsuccess\n";
}
Gkcha0z wrote:
I am very new to programming c++

Don't joke with us! That kind of code NEVER comes from a complete begginner! As for efficiency, 2 words: no system()!
Last edited on
Switch cases are much more efficient, though you can't use them with strings. The switch basically triggers a "jump to" command, which upon evaluating the switch, jumps to the proper case. There is no "is it this? is it this? is it this?"

But it is nice to have the string. You could list out possible items and then ask for a number, then use the switch on the number.

What Wazzak said, don't call main(). Do something like this:

Edit: keep forgeting that continue is a keyword...
1
2
3
4
5
6
7
8
9
10
11
12
13
bool searchEngine = true;
string choice;
while (searchEngine)
{
  cout << "Make a choice: ";
  getline(cin, choice);
  if (choice == "Something Interesting")
    // Do cool things
    ;
  else if (choice == "quit")
    searchEngine = false;
}
cout << "Have a nice day!"


Other optimizations include the fact that you call cls, and get() for every case. You can put these after the conditional:
1
2
3
4
5
6
7
8
if (ids == "dharok") {
  dharok();
}
else if (ids == "greataxe") {
  dharok();
}
cin.get();
system("cls");
Last edited on
Switch cases are much more efficient, though you can't use them with strings

Wrong! You most deffinetly CAN use it with strings! I belive you meant you can't use it with c-strings, which are in every way deprecated, except for constructing std::strings
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 <string.h>
#include <tchar.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;


void dharok()
{
     int dharok[8] = {4716, 4717, 4718, 4719, 4720, 4721, 4722, 4723}, x=0, y=0;
	 string Names[4] = {"Dharok's Helm: ", "Dharok's GreatAxe: ", "Dharok's PlateBody: ", "Dharok's PlateLegs: "};

	 	 for(int Loop=0;Loop<4;Loop++)
	 {
		 cout << Names[x] << dharok[y] << " - Noted: " << dharok[y++] << endl;
		 x++; y++;
	 }
}


void god()
{
	int Godswords[8] = {11694, 11695, 11696, 11697, 11698, 11699, 11670, 11671}, x=0, y=0;
	string Names[4] = {"Armadyl Godsword: ", "Bandos Godsword: ", "Zamorak Godsword: ", "Saradomin Godsword: "};

		 	 for(int Loop=0;Loop<4;Loop++)
	 {
		 cout << Names[x] << Godswords[y] << " - Noted: " << Godswords[y++] << endl;
		 x++; y++;
	 }
}


int main()
{

string ids;

  cout<<"Search for your item(only currently supports Godswords LOL).\n";
  cin>> ids;
  cin.ignore();

	if (ids == "dharok" || ids == "greataxe") {
	    dharok();
	    cin.get();
	    system("CLS");
	    main();
	}
	else if (ids == "godsword" || ids == "god" || ids == "armadyl" || ids == "bandos" || ids == "zamorak" || ids == "saradomin") {
	    god();
	    cin.get();
		system("CLS");
	    main();

	}
  cin.get();
  main();
}



I didnt read any of the code other than ur original peacie so there might be even more stuff you can do but this is what i got. For ur functions theres nothing much you can do to make it shorter but i still put a differnt way....

ps: || == or
Last edited on

viliml wrote:
--------------------
Gkcha0z wrote:
I am very new to programming c++
---------------------------------------------------
Don't joke with us! That kind of code NEVER comes from a complete begginner! As for efficiency, 2 words: no system()!


Dude i consider myself to still be quite a beginner, the more i learn about C++ the more i find out how large the language is and how much more there is to learn.
Don't give people hassle for thinking they're still a beginner because we all are. (for a start this is the reason we all post under the forum labelled "Beginners"
Last edited on
Wrong! You most deffinetly CAN use it with strings! I belive you meant you can't use it with c-strings, which are in every way deprecated, except for constructing std::strings


Actually, not wrong. A switch accepts constant integral types, of which a string is not. Try it first.

C-strings are in no way depreciated, especially the case when you need a wchar_t string. Or also using any graphical library that can print text. Or trying to read text from a binary file.

Last edited on
Thanks guys! So appreciated. But is there anyway I could have a sort of database for the ids and names and call on them when for example
"dh" is searched it will bring up "dharok's greataxe" ect
Also when I compile Johnnyj2j's code it produces errors which are caused by the string not being declared?
Indeed, a file. At that point you probably don't want to use an array because then you need more memory management skills. Use a vector instead.


The simplest thing I can think of is to create a file like so:
dharok's_names: Dharok's_Helm Dharok's_GreatAxe Dharok's_PlateBody Dharok's_PlateLegs

Note that anything you want as a string has no spaces. This is just to be simple, if you want spaces then you need some sort of parsing method. At some point you might want more than one line, so this complicates things more.

This file, named "strings.txt", is accessed like so:
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
#include <string>
#include <vector>
#include <iostream>
using namespace std;

int main(void)
  vector<string> words;

  // try to open a file
  ifstream input("strings.txt");

  // quit if it isn't open
  if (!input.is_open())
  {
    cout << "I couldn't open the file\n";
    return 1;
  }

  // read from the file
  string temp;
  while (input >> temp) words.push_back(temp);
 
  // close the file
  input.close();

  // output your vector

  for (int i = 0; i < words.size(); i++)
  {
    cout << words[i] << endl;
  }

  //finish your program

  cin.get();
  return 0;
}


It's all about getting things to work, and then using them until you need them to work better :)
Last edited on
Thank you! I will try to transfer the code over.
O sorry about the error..... one of the declerations was missing i gues i didnt coppy all my code....
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
#include <iostream>
#include <string>
#include <windows.h>
#include <string.h>
#include <tchar.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;



void dharok()
{
     int dharok[8] = {4716, 4717, 4718, 4719, 4720, 4721, 4722, 4723}, x=0, y=0;
	 string Names[4] = {"Dharok's Helm: ", "Dharok's GreatAxe: ", "Dharok's PlateBody: ", "Dharok's PlateLegs: "};

	 	 for(int Loop=0;Loop<4;Loop++)
	 {
		 cout << Names[x] << dharok[y] << " - Noted: " << dharok[y++] << endl;
		 x++; y++;
	 }
}


void god()
{
	int Godswords[8] = {11694, 11695, 11696, 11697, 11698, 11699, 11670, 11671}, x=0, y=0;
	string Names[4] = {"Armadyl Godsword: ", "Bandos Godsword: ", "Zamorak Godsword: ", "Saradomin Godsword: "};

		 	 for(int Loop=0;Loop<4;Loop++)
	 {
		 cout << Names[x] << Godswords[y] << " - Noted: " << Godswords[y++] << endl;
		 x++; y++;
	 }
}


int main()
{

string ids;

  cout << "Search for your item(only currently supports Godswords LOL).\n";
  cin >> ids;
  cin.ignore();

	if (ids == "dharok" || ids == "greataxe") {
	    dharok();
	    cin.get();
	    system("CLS");
	    main();
	}
	else if (ids == "godsword" || ids == "god" || ids == "armadyl" || ids == "bandos" || ids == "zamorak" || ids == "saradomin") {
	    god();
	    cin.get();
		system("CLS");
	    main();

	}
  cin.get();
  main();
}
Pages: 12