1

I'm trying to only input the strings that start with A and if other letters is used giving a error message. I've tired this:

#include <iostream>
 #include <bits/stdc++.h>
 using namespace std;

 int main(){
     char str[30];
     if(str[0] == "A"){
         cin.get(str, 30);
     }else{
         cout<<"Any name other than starting from A isn't supported."<<endl;
    
     cout<<str<<endl;
     return 0;  
 }
 }

I get this error: trialz.cpp: In function 'int main()': trialz.cpp:7:18: error: ISO C++ forbids comparison between pointer and integer [-fpermissive] 7 | if(str[0] == "A"){ | ^~~ I'm a beginner and your help will be much appreciated!!

Ayerror
  • 11
  • 1
  • 3
    Not only are the types wrong (comparing a `const char*` to `char`), the logic is backwards. You're comparing *before* even reading data. `str` contents are indeterminate, so even if you fix the first problem with `if(str[0] == 'A')` you're still invoking undefined behavior. If you're a beginner I cannot stress enough the importance of getting [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) (or three) as a foundation for your learning. – WhozCraig Jul 21 '21 at 05:20
  • fix curly brackets. and you can do like this: if(str[0] == 'A') if you want to use "string" instead of "char", so you can do like this: string str ="Ahello"; if(str.at(0) == 'A') – kfc Jul 21 '21 at 05:20
  • The first operation you do on the string is test if the first character is an `'A'`. If it is an `'A'`, your code THEN reads a string from the user. You need to change the order - read the string first and THEN test its contents. Also, `"A"` is a string literal, and `'A'` is a single letter - you can't use a string when a single character is needed, or vice versa. Also, consider using `std::string` instead of a raw array of `char`. – Peter Jul 21 '21 at 05:44
  • If you do not code-golf better heed https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h – Yunnosch Jul 21 '21 at 06:11

1 Answers1

3

you can't stop user from entering the string starting with 'A' but you can check after he entered , whether it starts with 'A' or not like this

#include <iostream>
#include <string>

int main()
{
   std::string s;
   std::getline(std::cin, s);
   
   if(s.front()=='A' || s.front()=='a'){
      std::cout<<"Any name other than starting from A isn't supported.\n";
   }else{
      std::cout<<s;   
   }

   return 0;
}
User
  • 572
  • 2
  • 10
  • Hi User. Please not that consistently indented code probably has higher chances on upvotes. – Yunnosch Jul 21 '21 at 06:09
  • An interesting example of incorrect (untested) code that gets upvoted. The condition inside `if` does not match the code it controls. Also, string `s` does not contain the end-of-line marker, so `<< "\n"` in the else clause would be welcome. Also, there's no protection against `front()` being invoked on empty `s`. – zkoza Jul 21 '21 at 07:30
  • the idea was to tell the way to solve the problem not to handle all edge cases here – User Jul 21 '21 at 07:43