LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   c++ cout not working :( (https://www.linuxquestions.org/questions/programming-9/c-cout-not-working-211354/)

Longinus 07-30-2004 02:02 AM

c++ cout not working :(
 
hello

i am new at this programming stuff and not that good either lol

but when i compile and run my program the cout << "Enter word to rotate: " doensnt show up on the screen. but you are able to type words in and hit enter and it will perform the rotate function, also the program never ends.

Code:

#include <iostream>

using namespace std;
       
char rotate(char *letter);                       

int main(){
        cout << "Enter word to rotate: ";
       
        char word[256];
        cin >> word;
       
        int len = strlen(word);
       
        for(int a = 0; a < len; a++)
                cout << rotate(&(word[a]));

        getchar();
        return 0;
}

//define function
char rotate(char *letter){
       
        char rep;
        char *prep = &rep;
        for(char a = 'a'; a <= 'z'; a++){
                if(*letter == a)
                        *prep = a + 13;
        }

        for(char a = 'A'; a <= 'Z'; a++){
                if(*letter == a)
                        *prep = a + 13;
        }       
       
        return rep;
}

anyone have any suggestions?

thanks

barisdemiray 07-30-2004 02:31 AM

Are you trying to change lowercases to uppercases and vice-versa? If so, the difference between lower ASCII and upper ASCII isn't 13, it's 32 and you must extract 32 when you change lowercase to uppercase. Your code works like this:

//define function
char rotate(char *letter){

char rep;
char *prep = &rep;
for(char a = 'a'; a <= 'z'; a++){
if(*letter == a)
*prep = a - 32;
}

for(char a = 'A'; a <= 'Z'; a++){
if(*letter == a)
*prep = a + 32;
}

return rep;
}

Output..

[baris@rhinox]$ ./a.out
Enter word to rotate: DeNeMe
dEnEmE[baris@rhinox]$

No idea about not working cout.. everything looks fine to me..

tamtam 07-30-2004 08:10 AM

instead of <iostream> try <iostream.h>

and if that doesnot work then you could use...

using::std cout;
using::std cin;

TamTam

barisdemiray 07-30-2004 09:26 AM

Quote:

Originally posted by tamtam
instead of <iostream> try <iostream.h>

and if that doesnot work then you could use...

using::std cout;
using::std cin;

TamTam

If you include iostream.h then there is no need to write using::std bla bla, because required libraries will already be included. But when you use new-style iostream (namespaces), you need to write explicitly what you use.

Code:

using namespace std;
all the I/O functions from standart namespace or

Code:

using::std cout;
only cout function from standart namespace. Hope these helps.

Longinus 07-30-2004 05:01 PM

well what i was doing was trying to rotate 13 letters in the alphabet of the orignal characters

so if the letter was like 'a' then it would count 13 letters up the alphabet and the result would be the letter 'n'

and as you can see in my source, i am using the new iostream with the using namespace std;

so i am baffled

asb 07-30-2004 05:40 PM

I am about as new at C++ as you. I always used std::cout untill I found out about the using namespace option. So you can try that. It will get very annoying, adding all those std:: when doing alot of coding. After finding out about using namespace, I believe that I read that the compiler will default to namespace std if it doesn't recognize a command. After this, I tried it and it works fine, so there is a second thing to try. Alot of times I don't bother with using namespace std or std:: anymore (and I realize that this is probably considered poor coding).

lyle_s 07-30-2004 11:31 PM

Quote:

Originally posted by Longinus
well what i was doing was trying to rotate 13 letters in the alphabet of the orignal characters so if the letter was like 'a' then it would count 13 letters up the alphabet and the result would be the letter 'n'
It works like a charm for me as long no letters higher than "m" are used.

The concept you're missing is that ASCII doesn't wrap back to "a" after "z". Any letters after "m" will have to be counted 13 letters down the alphabet.

It's strange you're not getting any output from std::cout. How are you running the program? At the command line, using an IDE of some kind, etc?

Lyle

bruce ford 07-31-2004 04:22 AM

hi,

try using flush after your first cout to make the string show up at once:


Code:

cout << "Enter word to rotate: " << flush;
bruce

Longinus 08-01-2004 04:37 PM

aight ok this is strang.... i made another file that has the SAME EXACT source as my orignal rot13.cc except this time i named it bla.cc....
- now the cout works but this time any letter after 'm' gets a totally diff asci character as lyle_s said.
- before the cout didnt work but it did as i wanted to so that even a letter after 'm' rotated back to the beginning of the alphabet....O_O (according to ascii this is wrong, even though this is what i wanted)

so i guess everything should be working as they should be now

so now my last question would be is how do i rotate a letter 13 alphabets while staying in the alphabet, so as i wont end up with other funky characters like {,./#$%

thanks for the help

psiakr3w 08-01-2004 05:26 PM

inside your rotate function, replace the statement following the two ifs with the following:

int c = a - 'a';
int d = (c + 13) % 26;
*prep = d + 'a';

c is just the index of the letter in the alphabet, and d is doing some modulo arithmetic action.

Longinus 08-01-2004 07:32 PM

ah thanks psiakr3w


All times are GMT -5. The time now is 03:22 PM.