#Call function again

79 messages · Page 1 of 1 (latest)

dawn arrow
#

How i can call function again inside another?
Exaple: cpp #include<cstdlib> #include<iostream> namespace huh { int func1(char ask) { cout << "Type something sigma here: "; cin >> ask; } int main() { if (ask = "sigma" or "Sigma") { cout << "Very sigma!" << endl; /*How do i call func1 again?*/ } } {

vapid mossBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

lilac ruin
#

What do you mean by "call again"? You don't call func1() at all

tepid hawk
#

huh::func1(//argument);

harsh basalt
#

u made ur function but never called it

#

also it's an int function returning nothing

tepid hawk
#

The main function is also inside the namespace huh

harsh basalt
#

you only run the main function

#

if you want the function to be called, you must call it in the main function

tepid hawk
#

You should read about functions and namespace from learncpp since there are a lot of issues tbh

dawn arrow
lilac ruin
#

You've still never called func1().

harsh basalt
#
#include<cstdlib>
#include<iostream>
namespace huh {
  int func1(char ask) {
    cout << "Type something sigma here: ";
    cin >> ask;
    return 0;
  }
  void func2() {
    cout << "Type something sigma here: ";
    cin >> ask;
  }
  int main() {
    if (ask = "sigma" or "Sigma") { 
      cout << "Very sigma!" << endl;
      /*How do i call func1 again?*/
      int res = func1('\0'); // res equals 0
      func2();
    }
  }
{
dawn arrow
#

how do i call functions then because my book doesn't say that or i didn't read abt it

harsh basalt
#

thats how

dawn arrow
tepid hawk
#

A book that doesn’t even explain how to call functions is pretty bad

harsh basalt
#

yea

#

lmao

dawn arrow
lilac ruin
#

@dawn arrow I'm not being mean, but your original question just really doesn't makes sense.

You asked us how to call a function again, but you've never even called it once, and now you're mentioning that you don't know what calling a function means or how to do it

dawn arrow
#

explaining its not about me

lilac ruin
#

So it's hard to determine what you're really asking here.

dawn arrow
#

i`ll try to explain better.

dawn arrow
lilac ruin
#

Ok, no worries. Let's try again. What your code snippet, and what are you trying to ask?

dawn arrow
#

Okay so, im very new in C++ and im trying to make some type of CMD with custom commands and im need to take user`s command again if user type wrong command

lilac ruin
#

That makes sense! So what have you got so far

dawn arrow
#
#include<iostream>
#include<cstdlib>
using namespace std;
int start(char ask) {
    cout << "Hello! This is MyCMD." << endl;
    cout << "CMD: ";
    cin >> ask;
}
int main() {
    system("chcp 1251>nul");
    system("title MyCMD");
    if (ask = "Help" or "help") {
        cout << "Here the list of avaliable commands: \nVersion -- shows current version of MyCMD" << endl;
    }
    else {
        cout << "Wrong command! Enter 'help' to view list of avaliable commands" << endl;
        for 
    }

    return 0;
}```
#

so

#

what i have to do to make it work properly?

lilac ruin
#

So going step by step, there are several issues here

#
  1. int start(char ask) { ... } is defining a new function, which is never used everywhere. That can just be deleted.
#
  1. ask = "Help" doesn't work the way you're expecting. It's not comparing ask to "Help". You need to use == for comparisons. This = is unconditionally assigning "Help" to the ask variable
tepid hawk
#

The or help is also not going to work

lilac ruin
#
  1. if (ask == "Help" or "help") still wouldn't work as you expect. You think it's saying if ask is equal to either one, but it's not. You would need if (ask == "Help" or ask == "help")
tepid hawk
#

lol, I spoiler it

lilac ruin
#
  1. Near the end, you have a dangling for that's unused. Frankly, I'm surprised it's not a syntax error that fails to compile.
#
  1. You use the ask variable several times, but you haven't declared it anywhere
dawn arrow
#

ok ty

#
#include<iostream>
#include<cstdlib>
using namespace std;
int main() {
    system("chcp 1251>nul");
    system("title MyCMD");
    char ask;
    cout << "Hello! This is MyCMD." << endl;
    cout << "CMD: ";
    cin >> ask;
    if (ask == "Help" or ask == "help") {
        cout << "Here the list of avaliable commands: \nVersion -- shows current version of MyCMD" << endl;
    }
    else {
        cout << "Wrong command! Enter 'help' to view list of avaliable commands" << endl;
    }

    return 0;
}```
lilac ruin
#

That's a start!

dawn arrow
#

incompatible operand types ("char" and "const char") and ==: no conversion of "const char [5]" to "int"

lilac ruin
#

Now the next issue here is that ask is a single char. It's not a string. So ask == "Help" can't possibly work, as ask can only hold 1 character (e.g. 'H' or 'h'

dawn arrow
#

oohhh

lilac ruin
#

Precisely. A const char [5] is an array of 5 characters, which is how that "Help" is being interpreted. You can't compare that string to your single character (which is being implicitly promoted to int, which is why that comes up in the error message)

dawn arrow
#

so, how i can solve this?

#

make an array?

lilac ruin
#

You can define ask as a C string:

std::string ask;
dawn arrow
#

oh

lilac ruin
#

(you can use more primitive C types like const char[] or const char *, but that way lies madness, so I didn't show that)

dawn arrow
#

thank you, now i feel more enlightened

lilac ruin
#

🙂

dawn arrow
#

okay

#

done but it still says that ==: no conversion of "const char [5]" to "int"

lilac ruin
#

where?

dawn arrow
#

nvm

#

okay, we`re done with bug fixes

#

lets return to the main question

#

in other languages i can just put everything in loop

tepid hawk
#

If you don’t mind, could you mention the book you’re using to learn C++. There are a lot of bad resources when it comes to programming and using any of them can hold you back from learning efficiently.

dawn arrow
#

so "CMD:" with input will appear every time

dawn arrow
dawn arrow
timber pumice
#

Why read someone else when you can read bjarne stroustrup himself 🤔🤔

tepid hawk
dawn arrow
#

4.9

#

rating

tepid hawk
#

Couldn’t find it from where I’m at

vapid mossBOT
#

@dawn arrow Has your question been resolved? If so, type !solved :)

remote jacinth
#

learncpp.com is the usual recommendation around here. Very comprehensive and up-to-date. It may fill in the many gaps you are not seeing.

dawn arrow
#
#include<iostream>
#include<cstdlib>
using namespace std;
int main() {
    system("chcp 1251>nul");
    system("title MyCMD");
    string ask;
    while (true) {
        cout << "CMD: ";
        cin >> ask;
        if (ask == "Help" or ask == "help") {
            cout << "Here the list of avaliable commands: \nVersion -- shows current version of MyCMD" << endl;
        }
        else {
            cout << "Wrong command! Enter 'help' to view list of avaliable commands" << endl;
        }
        if (ask == "Version" or "version") {
            cout << "Current version is 0.1" << endl;
        }
        else {
            cout << "Wrong command! Enter 'help' to view list of avaliable commands" << endl;
        }

    }
    return 0;
}```
#

it was so easy

#

bruh

#

!solve

vapid mossBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity