简体   繁体   中英

How can i get coordinates in C++?

I want to get coordinates x y in one line.

But x is char type [ a to j ] including a and j ; y is int type [ 1 to 10 ] including 1 and 10 .

Example: cout>>"Enter coordinates ex[e4] then press enter/n";

User should write: d5 and press enter.

Then i have to separate two input letter(character) and number.

I think that this could be done with getline or getchar

Can someone explain to me if this is possible and how to this?

Note that the couple letternumber (without space ex: f3) must be separated for two different function one based on char type the other one bases on int type

Thanks for all the answers. Don't worry, you're not solving my homework, I'm programming a little naval battle game :-) I had no idea on how to do that, all I know is just scholastic experience.

Here the code: This function checks the char value:

    int nx;
    nx=x; //Trasfromazione char x in int (NumeroX)
    switch(x) {
        case 97 ... 106:{ //Codice ascii a,b,c...
            return true;
            break;
        }
    default:{
        cout<<"\n---Lettera non valida\a\n\n";
        return false;
        break;
    }
    }
} 

this function checks the int value:

bool checknum(int y){
    bool v=false;
    switch(y) {
        case 1 ... 10 :{
            v=true;
            break;
        }
    default:{
        cout<<"\n---Numero non valido\a\n\n";
        return false;
        break;
    }
    }
    return v;   
}

and this is the code which calls two function:

do{
    cout<<"\n-Inserisci lettera numero\n>";
    cin>>x>>y;
}while(((checknum(y))==false) or((checklett(x))==false) );

if my input is two letters this cause an eternal loop

05-02-2020 dd-mm-yyyy Edit for @Tedlyngmo

With this code it seems to work:

do{
    cout<<"\n-Inserisci lettera e numero\n>";
    cin>>x>>y;
    cin.clear();    
}while((checklett(x)==false)or(checknum(y)==false));

do you know why?

A simple way to achieve what you ask is by reading the input with cin in a char[2] , so the letter will be stored in char[0] and the number in char[1] , or you can read it in separated variables, with a char storing the letter and an int storing the number:

int main()
{
    char s[2];
    // In a char array
    printf("Enter coordinates: ");
    std::cin >> s;

    // In separated char and int variables
    char a;
    int b;
    printf("Enter coordinates: ");
    std::cin >> a >> b;
    return 0;
}

This is how you can do it:

std::cout << "Enter coordinates ex[e4] then press enter/n";
char x;
int y;
std::cin >> x >> y;

You could use the formatted input capabilities of any std::istream (like std::cin ).

#include <iostream>

int main() {
    char letter;
    int number;

    if(std::cin >> letter >> number) {
        std::cout << "success. got letter " << letter << " and number " << number << '\n';
    } else {
        // failure
    }
}

Edit: The updated question now shows problem when std::cin enters a failed state. Here's one way to clear that state and to throw away garbage characters still in the buffer.

#include <iostream>
#include <limits>

// A function template to read a value from a stream and to validate it
template<typename T>
bool get_val(std::istream& is, T min, T max, T& out) {
    if(is >> out) {
        return out >= min && out <= max;
    }
    return false;
}

int main() {
    char letter;
    int number;

    while(true) {
        std::cout << "Enter coordinates: ";
        if(get_val<char>(std::cin, 'a', 'j', letter) &&
           get_val<int>(std::cin, 1, 10, number))
        {
            // success
            break;
        } else {
            // failure
            if(std::cin.eof()) {
                // end of file, the user is not there anymore
                std::cout << "Abort...\n";
                return 1;
            }
            std::cout << "Invalid input\n";
            // clear error state
            std::cin.clear();
            // clear input buffer to newline
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        }
    }

    std::cout << "Got letter " << letter << " and number " << number << '\n';
}
std::string str;
std::getline(std::cin, str);
std::cout << str; 

then do what you want with your string, eg https://en.cppreference.com/w/cpp/string/basic_string/stol from first letter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM