Need a hand.

I need this code to work desperately, this is my code, (keep in mind some of the actual code has been replaced with comments like in the first if section(you'll see))


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
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main()
{
  string yourname;
  string greeting1 ="Hey";
  string greeting2 ="hey";
  string greeting3 ="Sup?";
  string greeting4 ="Hello";
  string input;
    
  cout << "Welcome, please enter your name" << endl;
  cout << ">";
  getline(cin, yourname);
  cout << "Hello " << yourname <<". I will try to do my best to\nanswer anything you need help with" << endl;
  if (userhasinputanamethendon'taskthemtoagain);
  do {  //execute this code in an endless loop once user has given themself a name
    cout << ">";
    cin >> input;
      
    if (input == greeting2){
      cout << "How's it going?" << endl;

    } else if(input == greeting3){
      cout << "Not much, waiting for someone to talk to" << endl;

    } else if(input == greeting4){
      cout << "Hi " <<yourname<<". My name is ARC" << endl;
    }
    main();
  } while (input != input);
//execute this code if the user typed something that cannot be answered
    cout << "I'm sorry I didnt catch that, try asking in a different way." << endl;

} 
input != input

Can you think of any possible value of input which will not be the same as input? Bearing in mind that they're the exact same variable.

What does your code not do that you think it should? For all we know, this is all exactly what you want. You have to ask us a question if you want an answer.
Last edited on
it needs to ask for the users name then once it has the name, don't ask for it again, once that is finished, execute the second part of the code and after a user has entered greeting such as hi or hey, re-enter the same part of code so that after their question is answered, they may ask another question, The last part of code should be executed if the user types an invalid greeting or response.
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
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main()
{
  string yourname;
  string greeting1 ="Hey";
  string greeting2 ="hey";
  string greeting3 ="Sup?";
  string greeting4 ="Hello";
  string input;
    
  cout << "Welcome, please enter your name" << endl;
  cout << ">";
  getline(cin, yourname);
  cout << "Hello " << yourname <<". I will try to do my best to\nanswer anything you need help with" << endl;
  do {  //execute this code in an endless loop once user has given themself a name
    cout << ">";
    cin >> input;
      
    if (input == greeting2){
      cout << "How's it going?" << endl;

    } else if(input == greeting3){
      cout << "Not much, waiting for someone to talk to" << endl;

    } else if(input == greeting4){
      cout << "Hi " <<yourname<<". My name is ARC" << endl;
    }
    else
     {
     cout << "I'm sorry I didnt catch that, try asking in a different way." << endl;
}
  } while (true);  

} 


This allows the user anything at all for a name.

Do not ever do this: main();
ok thank you very much, just a noob trying to make it work
,,,
where did the true come from in while (true)
Last edited on
I would also like it to be possible to enter his or her responses in uppercase or lowercase without it being picky.
true is what is known in C++ as a "boolean literal". In the same way that you're allowed to write the number 7, for example, in your C++ code, you may write the boolean literals.

The do-while loop will keep going round as long as the condition in while (condition); is true. Simply putting while (true); is the easiest way of ensuring that condition is always true.
Last edited on
I would also like it to be possible to enter his or her responses in uppercase or lowercase without it being picky.


A common way to deal with this is to turn everything entered, no matter what case it is, into lowercase, and have your comparison strings in lowercase.
how do i do that?? with tolower?? but I hear thats only for chars,
and I know that true is a boolean literal but I mean what condition are you declaring as true?? anything thats not in the while loop??


this is my code right now,, totally ineffective but it works

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
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main()
{
  string yourname;
  string greeting1 ="Hey";
  string greeting2 ="hey";
  string greeting3 ="Sup?";
  string greeting4 ="Hello";
  string response1 ="good";
  string response2 ="Good";
  string response3 ="GOOD";
  string response4 ="Bad";
  string response5 ="bad";
  string response6 ="BAD";
  string input;
    
  cout << "Welcome, please enter your name" << endl;
  cout << ">";
  getline(cin, yourname);
  cout << "Hello " << yourname <<". I will try to do my best to\nanswer anything you need help with" << endl;
  do {  //execute this code in an endless loop once user has given themself a name
    cout << ">";
    getline(cin, input);
      
    if (input == greeting2){
      cout << "How's it going?" << endl;
      cout << ">";
      getline(cin, input);
      if (input == response1)
      {
      cout << "Good, I'm glad your doing well!" << endl;
      }
      else if(input == response2)
      {
      cout << "Good, I'm glad your doing well!" << endl;
      }
      else if(input == response3)
      {
      cout << "Good, I'm glad your doing well!" << endl;
      }
      else if(input == response4)
      {
      cout << "I'm sorry to hear that, cheer up!!" << endl;
      }
      else if(input == response5)
      {
      cout << "I'm sorry to hear that, cheer up!!" << endl;
      }
      else if(input == response6)
      {
      cout << "I'm sorry to hear that, cheer up!!" << endl;
      }
           

    } else if(input == greeting3){
      cout << "Not much, waiting for someone to talk to" << endl;

    } else if(input == greeting4){
      cout << "Hi " <<yourname<<". My name is ARC" << endl;
    }
    else
    {
     cout << "I'm sorry I didnt catch that, try asking in a different way." << endl;
    }
  } while (true);  

} 
Last edited on
and I know that true is a boolean literal but I mean what condition are you declaring as true?? anything thats not in the while loop??


while (condition);

condition will be evaluated as either true or false. If the code is simply while (true);, then true will be evaulated as true or false. Tell me, is true true, or is it false? (It's true).

condition does not mean whatever you think a condition is. Don't think in English; think in C++. In C++, condition is some statement - ANY statement - that can be assessed as either true (a.k.a. not zero) or false (a.k.a. zero).

true; is a statement, and it can be evaluated as either true or false.

tolower.... but I hear thats only for chars,

A std::string contains chars. Read and run this code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <string>
#include <iostream>

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

int main()
{
  string someString("EggS On TOAST");
  cout << someString << endl;
  cout << someString[0] << endl;
  someString[0] = tolower(someString[0]);
  cout << someString[0] << endl;
  cout << someString << endl;

  for (int i =0;i<someString.length();++i)
    {
      someString[i] = tolower(someString[i]);
    }
  cout << someString << endl;
  return 0;
}

Last edited on
Topic archived. No new replies allowed.