I could use some help!

Pages: 12
Nov 2, 2011 at 11:50pm
Hey guys, ive been learning c++ in the past few days and I want to make a simple program. You input a password if its correct, then you get to do an addition, if its wrong, you get booted. I would like to make it so that after you do your first equation you can type yes, or no ( or anything to replace no) If you type yes you can do another equation, if you type no you exit the program.
Heres 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
//infinite (hooefully) math loop

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

int main(){

      char myArray[50];
      
      cout << "Whats the password? ";
      
      cin.getline ( myArray, 50, '\n');
      
      if( !strcmp (myArray, "booker"))

{      
#include <iostream>

using namespace std;

     int a;
     
     int b;
     int e;
     
     cout << "Enter a number; ";
     cin >> a;
     
     cout << "Enter a second number: ";
     cin >> b;
     
     e = a+b;
     
     cout << "the sum is: " << e << "\n";
     
     char c;
     cout << "Continue?(Y/N)\n";
     cin >> c;
     }while(c == 'y');

     { else {
            strcpy( myArray, "invalid password!\n");
            
                   cout <<'\n';
            system("pause");
}


Also, I dont want you guys to do all the work for me... just post a snippet of code i should use and/or point me in the correct direction.

Thanks
Nov 2, 2011 at 11:52pm
looks like you're using a do while, and missing the do portion. also you don't need to include<iostream> or use using namespace std; again after your main()
Nov 2, 2011 at 11:55pm
ok, whats the code for do and where should i put it
Nov 3, 2011 at 12:24am
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
//infinite (hooefully) math loop

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

int main(){

      char myArray[50];
      
      cout << "Whats the password? ";
      
      cin.getline ( myArray, 50, '\n');
      
      if( !strcmp (myArray, "booker"))

    do{      // Added the do statement here.
#include <iostream>

using namespace std;

     int a;
     
     int b;
     int e;
     
     cout << "Enter a number; ";
     cin >> a;
     
     cout << "Enter a second number: ";
     cin >> b;
     
     e = a+b;
     
     cout << "the sum is: " << e << "\n";
     
     char c;
     cout << "Continue?(Y/N)\n";
     cin >> c;
     }while(c == 'y');

     { else {
            strcpy( myArray, "invalid password!\n");
            
                   cout <<'\n';
            system("pause");
}


Other than the strikethrough and the new 'do' I made no changes.

Edit:

The reason that I specifically mentioned that I made no other changes is that this code still has some serious bugs. Specifically, start counting brackets.
Last edited on Nov 3, 2011 at 12:26am
Nov 3, 2011 at 12:26am
thank you verry much :p
I got some help on another forum and the guy who gave me the do while forgot the do
Actually, im getting an error on the
}while(c == 'y');
part any ideas?

here are the errors:

C:\Users\Public\Documents\codes!\My codes\password test loop.cpp: In function `int main()':
C:\Users\Public\Documents\codes!\My codes\password test loop.cpp:39: error: `c' undeclared (first use this function)
C:\Users\Public\Documents\codes!\My codes\password test loop.cpp:39: error: (Each undeclared identifier is reported only once for each function it appears in.)

C:\Users\Public\Documents\codes!\My codes\password test loop.cpp: At global scope:
C:\Users\Public\Documents\codes!\My codes\password test loop.cpp:40: error: expected unqualified-id before "else"
C:\Users\Public\Documents\codes!\My codes\password test loop.cpp:40: error: expected `,' or `;' before "else"
Last edited on Nov 3, 2011 at 12:31am
Nov 3, 2011 at 12:40am
I think the problem is that you declared c within the do while loop, which means that it only has scope within the loop, and so can't be used for the loop condition. Trying moving char c; to pretty much anywhere after the start of your main, but before the do. Moving it to the line before or after char myArray[50]; would probably make the most sense. As ciphermagi said you're not finished yet, you need to be more careful with your brackets.
Nov 3, 2011 at 12:45am
wich brakets should i count? Sorry, im really new to c++ coding
Nov 3, 2011 at 12:54am
wich brakets should i count? Sorry, im really new to c++ coding


All of them. If you have '{', then you need the exact same number of '}'

As for the }while(c == 'y'); as Meerkat explained, it's out of scope. You need to declare it before you start the do-while loop.
Nov 3, 2011 at 1:02am
I'm really confused right now...
I have 3 { and 3 }
And by declairing do you like declare the it like int=c or something?
Nov 3, 2011 at 1:05am
A lot of IDEs will highlight the matching bracket, if yours doesn't even try copying your code into notepad++ (or equivalent) and make sure you change your language to c++, click on one bracket and both it and it's matching bracket will be highlighted. Make sure the brackets are the way you want them to be. The other thing which might not be too obvious to you is that you will need a '{' immediately after your if statement and a '}' immediately before your else, otherwise it won't know that the else belongs to that if.
Nov 3, 2011 at 1:05am
No. Put char c; at the top of the main() function.

Also, count the brackets again. I count 4 '{' and 2 '}'
Nov 3, 2011 at 1:09am
Declaring it is simply where you have written char c; just move this one line. Looking at the code ciphermagi post, move the contents of line 39 to line 12 and you should be good. When we are saying brackets we are meaning {} not ().
Nov 3, 2011 at 1:12am
yeah, i have char c; at line 12, no longer 39 and the brackets on my version (the one sitting in devc++ infront of me) has 3 of each bracket.

This being the version infront of me
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
//infinite (hopefully) math loop

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

int main(){

      char myArray[50];
      char c;
      
      cout << "Whats the password? ";
      
      cin.getline ( myArray, 50, '\n');
      
      if( !strcmp (myArray, "booker"))

   do{      

   int a;
     
     int b;
     int e;
     
     cout << "Enter a number; ";
     cin >> a;
     
     cout << "Enter a second number: ";
     cin >> b;
     
     e = a+b;
     
     cout << "the sum is: " << e << "\n";
     
     cout << "Continue?(Y/N)\n";
     cin >> c;
     }while(c == 'y');
     } else {
            strcpy( myArray, "invalid password!\n");
            
                   cout <<'\n';
            system("pause");
}


Last edited on Nov 3, 2011 at 1:14am
Nov 3, 2011 at 1:20am
As I said earlier, you need a '{' after your if statement to match it with your else statement, once you do this it should become obvious what is wrong. Also I know space isn't important from the compiler's point of view, but if you use it well spotting errors like this will become much more obvious. Let me know when you figure out the problem and I'll explain better.
Nov 3, 2011 at 1:21am
ohh ok, so i need to add a '{' after the if statement and that will throw off my { count
Nov 3, 2011 at 1:31am
Yup. If your IDE doesn't offer bracket highlighting I would really recommend getting notepad++ if only for this one purpose, also it's generally just better than standard notepad (I'm assuming your on a windows computer) I can just click on the opening bracket after your main and see that it matches up with the one before your else statement which is obviously wrong. Also generally when you open a bracket, indent everything after it until you get to the corresponding closing bracket, it will make your code into much more obvious blocks, and your much less likely to have both errors with mismatching brackets and it will also make the scope of your variables much more obvious, which was pretty much your two problems with this code.

EDIT: here's a link to download notepad++: http://notepad-plus-plus.org/download/v5.9.6.html
Last edited on Nov 3, 2011 at 1:31am
Nov 3, 2011 at 1:33am
I wouldn't use notepad++ as an IDE. If you're really looking to start learning, get Code::Blocks or similar. Something that's fully functional and can build and run your programs for you (even though it can't make a bin file).
Nov 3, 2011 at 1:40am
Sorry I didn't mean to suggest that you should use notepad++ as an IDE, I just mean that if his IDE doesn't already offer brackets highlightling having a quick look at the code in something like notepad++ that does can really help to make it more obvious which brackets a pairs. Also it's probably worth mentioning that in visual studio it doesn't bracket highlight (at least not by default) but if you click on one of the brackets you can press Ctrl+] and it will take you to the matching bracket. Not sure about other IDEs but I assume they will offer a similar feature.
Last edited on Nov 3, 2011 at 1:41am
Nov 3, 2011 at 1:41am
It's cool. The point is, though, as Meerkat said, development aids are very, very useful in debugging, especially for inexperienced users. You should look in to one.
Nov 3, 2011 at 1:42am
i allready have notepad++ and i use devC++.
But, what bracket should i make correspoding to the if bracket
Pages: 12