LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 07-30-2004, 03:02 AM   #1
Longinus
Member
 
Registered: Sep 2003
Distribution: Redhat 9.0 && Slackware 9.1
Posts: 420

Rep: Reputation: 30
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

Last edited by Longinus; 07-30-2004 at 03:04 AM.
 
Old 07-30-2004, 03:31 AM   #2
barisdemiray
Member
 
Registered: Sep 2003
Location: Ankara/Turkey
Distribution: Slackware
Posts: 155

Rep: Reputation: 30
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..
 
Old 07-30-2004, 09:10 AM   #3
tamtam
Member
 
Registered: May 2004
Distribution: Slackware.
Posts: 321

Rep: Reputation: 33
instead of <iostream> try <iostream.h>

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

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

TamTam
 
Old 07-30-2004, 10:26 AM   #4
barisdemiray
Member
 
Registered: Sep 2003
Location: Ankara/Turkey
Distribution: Slackware
Posts: 155

Rep: Reputation: 30
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.
 
Old 07-30-2004, 06:01 PM   #5
Longinus
Member
 
Registered: Sep 2003
Distribution: Redhat 9.0 && Slackware 9.1
Posts: 420

Original Poster
Rep: Reputation: 30
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
 
Old 07-30-2004, 06:40 PM   #6
asb
Member
 
Registered: Jul 2003
Location: portland
Distribution: debian testing
Posts: 68

Rep: Reputation: 15
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).
 
Old 07-31-2004, 12:31 AM   #7
lyle_s
Member
 
Registered: Jul 2003
Distribution: Slackware
Posts: 388

Rep: Reputation: 52
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
 
Old 07-31-2004, 05:22 AM   #8
bruce ford
Member
 
Registered: Jul 2004
Location: Munich, Germany
Distribution: Sun Solaris 8, SuSE 9.0
Posts: 43

Rep: Reputation: 15
hi,

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


Code:
cout << "Enter word to rotate: " << flush;
bruce
 
Old 08-01-2004, 05:37 PM   #9
Longinus
Member
 
Registered: Sep 2003
Distribution: Redhat 9.0 && Slackware 9.1
Posts: 420

Original Poster
Rep: Reputation: 30
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

Last edited by Longinus; 08-01-2004 at 05:39 PM.
 
Old 08-01-2004, 06:26 PM   #10
psiakr3w
LQ Newbie
 
Registered: Jul 2004
Posts: 27

Rep: Reputation: 15
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.
 
Old 08-01-2004, 08:32 PM   #11
Longinus
Member
 
Registered: Sep 2003
Distribution: Redhat 9.0 && Slackware 9.1
Posts: 420

Original Poster
Rep: Reputation: 30
ah thanks psiakr3w
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
`cout' undeclared in namespace `std' aw_wolfe Programming 1 04-30-2005 10:25 PM
cout conversion to binary... audibel Programming 3 03-12-2005 08:06 AM
undefined symbol cout Tazzie76 Linux - Software 8 12-10-2004 03:09 PM
C++ boolean in cout statement AMMullan Programming 2 06-08-2004 01:44 PM
cout queues miguetoo Programming 4 03-17-2004 03:02 AM


All times are GMT -5. The time now is 09:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration