-
newbie coding problem
Here is the code for the program I am having trouble with, I realize that with buffered input it doesn't stop when the @ symbol is entered. Thats not the problem. I'm using g++ and am getting no output after the input is entered? Thanks for any help given!
// upper lower case conversion prblm 1 pg 137
#include <iostream>
using namespace std;
#include <cctype>
int main()
{
char ch;
cout << "Changes lower case to upper and upper to lower case.\n";
cout << "Enter a phrase:";
cin.get(ch);
while (ch != '@')
{
if(isalpha(ch))
{
if(islower(ch))
ch=toupper(ch);
else
ch=tolower(ch);
cout << ch;
}
cin.get(ch);
}
return 0;
}
-
Why not just use -1 to end, since the program is meant to change the case to upper and lower then the program should stop or at least print an error message when it encounters a non alphabetic character.
Code:
// upper lower case conversion prblm 1 pg 137
#include <iostream>
#include <cctype>
using namespace std;
int main(void)
{
char ch;
cout<<"Changes lower case to upper and upper to lower case.\n";
cout<<"Enter a phrase (-1 to exit):";
cin.get(ch);
while(ch != '-1'){
if(isalpha(ch)){
if(islower(ch))
ch=toupper(ch);
else
ch=tolower(ch);
cout<<ch<<flush;
}else{
cout<<"input not valid!"<<flush;
cin.get(ch);
}
return 0;
}
-
Alphabetical
The problem description in c++ primer plus third edition, said use @ to end input, but I think you answered the problem anyhow though I hadn't yet learned to flush the buffer. It did say not to print digits entered just alphabetical chars. I would like to know how to do this w/o using somthing the book hasn't taught yet, and I don't understand why my version doesn't work.
QUOTE]Originally posted by Unregistered
Why not just use -1 to end, since the program is meant to change the case to upper and lower then the program should stop or at least print an error message when it encounters a non alphabetic character.
Code:
// upper lower case conversion prblm 1 pg 137
#include <iostream>
#include <cctype>
using namespace std;
int main(void)
{
char ch;
cout<<"Changes lower case to upper and upper to lower case.\n";
cout<<"Enter a phrase (-1 to exit):";
cin.get(ch);
while(ch != '-1'){
if(isalpha(ch)){
if(islower(ch))
ch=toupper(ch);
else
ch=tolower(ch);
cout<<ch<<flush;
}else{
cout<<"input not valid!"<<flush;
cin.get(ch);
}
return 0;
}
[/QUOTE]
-
This works fine for me
Code:
#include <iostream>
using namespace std;
#include <cctype>
int main()
{
char ch;
cout<<"Changes lower case to upper and upper to lower case.\n";
cout<<"Enter a phrase:";
cin.get(ch);
while (ch != '@'){
if(isalpha(ch) || isspace(ch)){
if(islower(ch))
ch=toupper(ch);
else
ch=tolower(ch);
cout<<ch;
}
cin.get(ch);
}
return 0;
}
-
I add the or isspace function as you did in the code. It compiles fine as it did before, but I still get no output. I'm using the g++ compiler in linux.
Where you using visual studio?
Here is a listing of my code once again cut and pasted.
// upper lower case conversion prblm 1 pg 137
#include <iostream>
using namespace std;
#include <cctype>
int main()
{
char ch;
cout << "Changes lower case to upper and upper to lower case.\n";
cout << "Enter a phrase:";
cin.get(ch);
while (ch != '@')
{
if(isalpha(ch) || isspace(ch))
{
if(islower(ch))
ch=toupper(ch);
else
ch=tolower(ch);
cout << ch;
}
cin.get(ch);
}
return 0;
}
-
sorry about the lack of indentation, thats the way the code appeared after it posted.
-
still not working
I still haven't gotten any output from my program. I even tryed cout << flush; at the end of the program as well as in the line cout << ch << flush; but am still getting no output. I guess flush is supposed to "flush the output buffer" really don't know though because this hasn't been taught yet in this book.
Here is a similar program from the book that works just fine when I compile it.
//ifelse.cpp pg208
#include <iostream>
using namespace std;
int main()
{
char ch;
cout << "Type, and I shall repeat.\n";
cin.get(ch);
while (ch != '.')
{
if (ch == '\n')
cout << ch;
else
cout << ch+1;
cin.get(ch);
}
cout << "\nPlease excuse the slight confusion.\n";
return 0;
}
this makes me think that the function calls ch=toupper(ch);
ch=tolower(ch);
are somehow causing the problem. Any ideas?
-
when I say no output I mean no output after I enter the phrase. common people doesn't anyone have an answer to this?
I thought maybe it had to do with the cin.get(ch) if it returned the eol char the last thing entered in the buffer that would be the first thing the next cin.get(ch) would find and there for not work, but I think it should returrn the very first char in the buffer not the last, being just a newbie I'm beginning to speculate wildly. PS II'd like to move on to the next problem but am stuck here
-
Apparently the problem has to do with flushing the buffer. If I cout << "\n another sentencte\n" at the end of the program the rest of the output is also flushed. I wonder why when I tryed cout << flush; it didn't work? Well solves that problem.
-
For some reason cout<< doesn't always work for me in different sections of my prog, it just doesn't display anything, now using printf() always works for me.
-
Hmm, cout << flush should flush the output buffer.
Try cout.flush(); not cout << flush.
Maybe you shoud include <iomanip>
try this code
Code:
#include <iostream>
#include <iomanip>
#include <cctype>
using namespace std;
int main()
{
char ch;
cout<<"Changes lower case to upper and upper to lower case.\n";
cout<<"Enter a phrase:";
cin.get(ch);
while (ch != '@'){
if(isalpha(ch) || isspace(ch)){
if(islower(ch))
ch=toupper(ch);
else
ch=tolower(ch);
cout<<ch;
cout.flush(); //flush the output buffer so it displays ****
}
cin.get(ch);
}
return 0;
}
You can use CODE inside []'s and /CODE inside [] to start and end a code block.