If anyone has been following the question that I sent in below, the problem has been solved by another root user. See below. ************************************** You need to include "iostream.h" I think... { cout << "Enter a comment" << endl << flush; char comment[200]; cin.get(comment,200); cout << "Your comment was: " << comment << endl << flush; } the output will be: root[0] .x comment.c (by root) Enter a comment (by you ) This is a nice comment (by root) Your comment was : This is a nice comment root[1] ****************************************** I have also found that cin.getline works a little better sometimes as it skips the \n at the end of the string. Interestingly, in the following code in a Root macro; printf("Enter a file name to load > "); cin.get(filenam,100,'\n'); printf("The file name is %s \n",filenam); printf("Enter hybrid serial number > "); scanf("%s",&h); printf("the hybrid number is %s \n",h); All characters in filenam (line 2) are read but the first character in h is not read (line 5). This problem is independent of whether the second read line is reading an integer, character etc. It is also independent of whether the second read is performed by a scanf or a cin.get or a cin.getline. However, if I swap the read lines (ie read h then read filenam) the problem is reversed. The first read from the command line is always ok, only the subsequent reads skip the first character. I've been told that this is a common problem with Root on NT and that the solution is to input one useless character first before inputing the actual value that is required. This obviously works but it's a bit annoying. Does anyone know if there is a fix to this ? Craig Everton On Thu, 1 Mar 2001, Craig Everton wrote: > > > I've just recently started using Root. I'm having a lot of problems with > what I thought would be very simple. One of my buttons calls the > function scanner_new() which puts a few prompts at the command line for > reading a serial number (integer) and a comment (string). See below; > > void scanner_new(){ > int h; > int i = 0; > char comment[200], a, cmt[200]; > char a; > > printf("New: enter hybrid serial number:"); > scanf("%d", &h); > > printf("Enter any comments:"); > gets(comment); > > printf("The comment is : %s \n",comment); > > e->SetHybrid(h,comment); > e->ReadLastInfo(); > e->ResetAll(); > } > > There is no problem with reading in the hybrid number but the line > > gets(comment); > > is not recognized. There is no error message, the script just skips to the > next line in the code. I thought that perhaps the stdio.h library may need > to be included explicitely but adding the line > > G__loadfile("stdio.h") doesn't help. > > > I've tried many fixes such as; > > printf("Enter any comments:"); > a=getchar(); > while (a != "\n") { > a=getchar(); > comment[i] = a; > putchar(a); > i++; > } > > AND > > printf("enter any comments"); > > while(cmt != "\n"){ > scanf("%s",cmt); > strcat(comment, " "); > strcat(comment, cmt); > } > > But "\n" and "\r" is never found so the while loop never exits. > > I've also tried > > > cmt = new TString(); > printf("Enter any comments:"); > cmt->ReadString(comment); > > > > But get the error > > Can't call TString::ReadString(comment) in current scope > > > I'd appreciate it if anyone could tell me where I'm going wrong. > > Thanks in advance > > > Craig > > -====================================================== > Craig Everton > Experimental Particle Physics (EPP) > School of Physics > University of Melbourne, Victoria > AUSTRALIA 3010 > Phone : (work) (613) 9344 5075 > (home) (613) 9387 6726 > Fax : (613) 9347 4783 > Email : everton@liszt.ph.unimelb.edu.au > ====================================================== > > >
This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:50:38 MET