473,491 Members | 3,350 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

scanf problems with strings and chars

9 New Member
Hi,

Im quite new to C++ and am trying to learn most of the commands.
So i wanted to make a Programm that has a normal mode and a non normal mode, kindof.
And i wanted to make a question, which the user has to answer to start normal or unnormal mode.
I think this code should show my idea.

The if command never Reacts to a j.
Why is that??

Expand|Select|Wrap|Line Numbers
  1.   char jon;
  2.  
  3.     printf("Should normal mode be executed?  [j/n]");
  4.     cout << endl;
  5.     scanf("%s",jon);
  6.     fflush(stdin);
  7.   if (jon == "j"){}
  8.   else{}
  9.  
Thanks
Sep 28 '11 #1
2 1627
Banfa
9,065 Recognized Expert Moderator Expert
Line 3 and 4, do not mix C and C++ io routines, you have no guarantee they are compatible. You are using C++ so use C++ cout << "Should normal mode be executed? [j/n]" << endl;

Line 5 This is C++ you should use cin not scanf. %s is for a C style string in an array of char but you provide a single char and not even a pointer to it.

Line 6 Never ever flush stdin. It is undefined behaviour.

Line 7 You can't do with a C style string in an array of char, you can do it with a single char but then you compare to a string constant which wont work. You can do this with a C++ std::string. You are using C++ do things the C++ way.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3.  
  4.   char jon;
  5.   std::string stuff;
  6.  
  7.   std::cout << "Should normal mode be executed?  [j/n]" << std::endl; 
  8.   std::cin >> jon;
  9.   std::getline(cin, stuff); // Clear rest of line from stdin
  10.  
  11.   if (jon == 'j' || jon == 'J')
  12.   {
  13.   }
  14.   else
  15.   {
  16.   }
  17.  
Sep 28 '11 #2
Noodles
9 New Member
Ohh,

Ok thanks i didn't know the command cin, this makes life a lot easier.
So thanks a lot!!!
Sep 29 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
2758
by: Venkat | last post by:
Hi All, I need to copy strings from a single dimensional array to a double dimensional array. Here is my program. #include <stdio.h> #include <stdlib.h>
3
4955
by: Ramprasad A Padmanabhan | last post by:
Hello all I want to read into a string an input from QUERY_STRING how do I ensure that scanf reads more chars into the string that it can hold eg { char* s1; char* data; long n;
4
8775
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
7
2673
by: happy | last post by:
I need to write a string field in a structure data type to a file using fprintf,fscanf. I failed to use the below syntax. Please I need to not go outside scanf,fprintf,printf . /* Book name ...
7
5620
by: arkobose | last post by:
hey everyone! i have this little problem. consider the following declaration: char *array = {"wilson", "string of any size", "etc", "input"}; this is a common data structure used to store...
25
3866
by: sravishnu | last post by:
Hello, I have written a program to concatanae two strings, and should be returned to the main program. Iam enclosing the code, please give me ur critics. Thanks, main() { char s1,s2;...
19
3077
by: pkirk25 | last post by:
I wonder if anyone has time to write a small example program based on this data or to critique my own effort? A file called Realm List.html contains the following data: Bladefist-Horde...
68
4550
by: stasgold | last post by:
Hello. I maybe reinvent the weel ... I'm trying to read positive integer number with the help of scanf, if the input value is not positive number but negaive one zero or char , i have to reread...
95
4956
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
51
2508
by: deepak | last post by:
Hi, For the program pasted below, scanf is not waiting for the second user input. Can someone suggest reason for this? void main() { char c;
0
6980
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5452
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4886
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3087
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1397
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
282
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.